
#######################################################################################################
# Autor: Jan Stuckatz + Jieun Lee
# Date: 2023/05/10
# Paper Title: Mobilization and Strategies: Comparing Trade Lobbying in the U.S. and Canada
# Replication of tables and figures in the appendix
######################################################################################################

# required libraries
library(lubridate)
library(knitr)
library(kableExtra)
library(ggplot2)  
library(lfe)
library(sandwich)
library(lmtest)
library(readr)
library(stargazer)
library(dplyr)

# set wd to where code is, if using RStudio
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

usa <- readr::read_csv("../data/usa_clients.csv")
can <- readr::read_csv("../data/can_clients.csv")



# input panel data, for subsetting to 2016 to 2020, USMCA
# USA
us_2016_2020 <- readr::read_csv("../data/panel_usa.csv") %>%
  filter(year_q %in% c("2016-Q1", "2016-Q2", "2016-Q3", "2016-Q4",
                       "2017-Q1", "2017-Q2", "2017-Q3", "2017-Q4",
                       "2018-Q1", "2018-Q2", "2018-Q3", "2018-Q4",
                       "2019-Q1", "2019-Q2", "2019-Q3", "2019-Q4",
                       "2020-Q1", "2020-Q2"))

# CAN
ca_2016_2020 <- readr::read_csv("../data/panel_can.csv") %>%
  filter(year_q_ch %in% c("2016-Q1", "2016-Q2", "2016-Q3", "2016-Q4",
                          "2017-Q1", "2017-Q2", "2017-Q3", "2017-Q4",
                          "2018-Q1", "2018-Q2", "2018-Q3", "2018-Q4",
                          "2019-Q1", "2019-Q2", "2019-Q3", "2019-Q4",
                          "2020-Q1", "2020-Q2"))

# Subset by CA and US clients between 2016 and 2020
usa <- usa %>% filter(!is.na(bvdid_final)) %>%
  filter(bvdid_final %in% unique(us_2016_2020$bvdid_final[us_2016_2020$usmca==1]))
can <- can %>% filter(!is.na(bvdid_final)) %>%
  filter(bvdid_final %in% unique(ca_2016_2020$bvdid_final[ca_2016_2020$usmca==1]))


#### Companies Lobbyng in Both CAN and USA
can$both <- ifelse(can$bvdid_final %in% usa$bvdid_final, 1, 0)
usa$both <- ifelse(usa$bvdid_final %in% can$bvdid_final, 1, 0) 

# get unique firms lobbying on both sides, check they are the same.
us_both_unique <- sort(unique(usa$bvdid_final[usa$both==1]))
ca_both_unique <- sort(unique(can$bvdid_final[can$both==1]))

# create df with unique clients on both sides and some descriptives
clients_both <- can %>% filter(both == 1) %>%
  select(bvdid_final, org_type, orbis_name_final, GUO_Name, GUO_bvdid) %>%
  distinct()


#### Table A2 ####
# read in US lobbying reports data
us_comms <- readr::read_csv("C:/Users/js.egb/Dropbox/Uni_Projects/us_can_nafta_lobby/submission/CPS/replication_material/data/usa_clients_comms.csv")

# aggregate at the client level, subset to 2016 to 2020
parl_us <- us_comms %>%
  # filter(report_year > = 2012) %>%
  filter(!is.na(bvdid_final), usmca == 1) %>%
  group_by(bvdid_final, orbis_name_final) %>%
  summarise(pres = sum(c(whouse+execpr), na.rm = TRUE),
            house = sum(house, na.rm = TRUE),
            senate = sum(senate, na.rm = TRUE),
            ustr = sum(ustr, na.rm = TRUE),
            doc = sum(doc, na.rm = TRUE ),
            dag = sum(dag, na.rm = TRUE),
            inst = sum(n_gov_entities, na.rm = TRUE),
            b_pres = ifelse(sum(c(whouse+execpr), na.rm = TRUE) > 0, 1, 0),
            b_house = ifelse(sum(house, na.rm = TRUE) > 0, 1, 0),
            b_senate = ifelse(sum(senate, na.rm = TRUE) > 0, 1, 0),
            b_ustr = ifelse(sum(ustr, na.rm = TRUE) > 0, 1, 0),
            b_doc = ifelse(sum(doc, na.rm = TRUE ) > 0, 1, 0),
            b_dag = ifelse(sum(dag, na.rm = TRUE) > 0, 1, 0),
            usmca = ifelse(sum(usmca, na.rm = TRUE) > 0, 1, 0),
            s_pres = sum(c(whouse+execpr), na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_house = sum(house, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_senate = sum(senate, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_ustr = sum(ustr, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_doc = sum(doc, na.rm = TRUE )/sum(n_gov_entities, na.rm = TRUE),
            s_dag = sum(dag, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE)) %>%
  filter(!is.na(bvdid_final)) %>%
  filter(bvdid_final %in% unique(us_2016_2020$bvdid_final[us_2016_2020$usmca==1]))


# Create Empty Shell DF
tab_a2 <- data.frame(`Institution` = c("President", "House",
                                        "Senate", "Trade Representative" , "Department of Commerce",
                                        "Department of Agriculture", " United States Total"),
                      `Share_Institutions` = rep(NA, 7),
                      `Unique_Clients_USMCA` = rep(NA,7),
                      stringsAsFactors = FALSE)

# Overall Client share of Institutions, Any agreemment
colnames(tab_a2) <- c("Institution", "Reports (Share)", "USMCA (Share)")

# Firm-level lobbying of Institutions, USMCA
us_usmca <- parl_us %>% filter(usmca == 1)
tab_a2$`USMCA (Share)`[1] <- paste0(sum(us_usmca$b_pres), " (", round(sum(us_usmca$b_pres)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a2$`USMCA (Share)`[2] <- paste0(sum(us_usmca$b_house), " (", round(sum(us_usmca$b_house)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a2$`USMCA (Share)`[3] <- paste0(sum(us_usmca$b_senate), " (", round(sum(us_usmca$b_senate)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a2$`USMCA (Share)`[4] <- paste0(sum(us_usmca$b_ustr), " (", round(sum(us_usmca$b_ustr)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a2$`USMCA (Share)`[5] <- paste0(sum(us_usmca$b_doc), " (", round(sum(us_usmca$b_doc)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a2$`USMCA (Share)`[6] <- paste0(sum(us_usmca$b_dag), " (", round(sum(us_usmca$b_dag)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a2$`USMCA (Share)`[7] <- length(unique(us_usmca$bvdid_final))

# How often does which institution get mentioned (out of all reports)
tab_a2$`Reports (Share)`[1] <- paste0(sum(parl_us$pres), " (", round(sum(parl_us$pres, na.rm = TRUE)/dim(us_comms[!is.na(us_comms$bvdid_final),])[1], 2), ")")
tab_a2$`Reports (Share)`[2] <- paste0(sum(parl_us$house), " (", round(sum(parl_us$house, na.rm = TRUE)/dim(us_comms[!is.na(us_comms$bvdid_final),])[1], 2), ")")
tab_a2$`Reports (Share)`[3] <- paste0(sum(parl_us$senate), " (", round(sum(parl_us$senate, na.rm = TRUE)/dim(us_comms[!is.na(us_comms$bvdid_final),])[1], 2), ")")
tab_a2$`Reports (Share)`[4] <- paste0(sum(parl_us$ustr), " (", round(sum(parl_us$ustr, na.rm = TRUE)/dim(us_comms[!is.na(us_comms$bvdid_final),])[1], 2), ")")
tab_a2$`Reports (Share)`[5] <- paste0(sum(parl_us$doc), " (", round(sum(parl_us$doc, na.rm = TRUE)/dim(us_comms[!is.na(us_comms$bvdid_final),])[1], 2), ")")
tab_a2$`Reports (Share)`[6] <- paste0(sum(parl_us$dag), " (", round(sum(parl_us$dag, na.rm = TRUE)/dim(us_comms[!is.na(us_comms$bvdid_final),])[1], 2), ")")
tab_a2$`Reports (Share)`[7] <- dim(us_comms[!is.na(us_comms$bvdid_final),])[1]

# rename cols
colnames(tab_a2) <- c("Institution", "Reports (Share)", "Clients (Share)")

options(knitr.kable.NA = '')
kableExtra::kable(tab_a2, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  caption = "Types of Insitutions lobbied in the United States, 2016-2020", label = "tab_a2") %>%
  footnote(general = "The table shows the types of institutions lobbied in Canada during the USMCA negotiations. This refers to the clients that report having lobbied or intended to lobby the respective governement institutions.", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c("hold_position"), font_size = 11) %>%
  add_header_above(c("", "United States USMCA Lobbying" = 2), bold = TRUE) %>%
  kableExtra::column_spec(1, width = "18em") %>%
  save_kable(file = "../output/table_a2.tex")


#### Table A3 ####

# MNCs Create Empty Shell DF
tab_a3 <- data.frame(`Institution` = c("President", "House",
                                            "Senate", "Trade Representative" , "Department of Commerce",
                                            "Department of Agriculture", " United States Total"),
                          `Share_Institutions` = rep(NA, 7),
                          `Unique_Clients_USMCA` = rep(NA,7),
                          stringsAsFactors = FALSE)

# Overall Client share of Institutions, Any agreemment
colnames(tab_a3) <- c("Institution", "Reports (Share)", "USMCA (Share)")

# Firm-level lobbying of Institutions, USMCA
us_usmca_mnc <- parl_us %>% filter(usmca == 1)%>%
  mutate(mnc = ifelse(substr(bvdid_final, 1, 2) == "US", 0, 1)) %>%
  filter(mnc == 1)
tab_a3$`USMCA (Share)`[1] <- paste0(sum(us_usmca_mnc$b_pres), " (", round(sum(us_usmca_mnc$b_pres)/length(unique(us_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a3$`USMCA (Share)`[2] <- paste0(sum(us_usmca_mnc$b_house), " (", round(sum(us_usmca_mnc$b_house)/length(unique(us_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a3$`USMCA (Share)`[3] <- paste0(sum(us_usmca_mnc$b_senate), " (", round(sum(us_usmca_mnc$b_senate)/length(unique(us_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a3$`USMCA (Share)`[4] <- paste0(sum(us_usmca_mnc$b_ustr), " (", round(sum(us_usmca_mnc$b_ustr)/length(unique(us_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a3$`USMCA (Share)`[5] <- paste0(sum(us_usmca_mnc$b_doc), " (", round(sum(us_usmca_mnc$b_doc)/length(unique(us_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a3$`USMCA (Share)`[6] <- paste0(sum(us_usmca_mnc$b_dag), " (", round(sum(us_usmca_mnc$b_dag)/length(unique(us_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a3$`USMCA (Share)`[7] <- length(unique(us_usmca_mnc$bvdid_final))

# How often does which institution get mentioned (out of all reports)
parl_us_mnc <- parl_us %>% filter(usmca == 1)%>%
  mutate(mnc = ifelse(substr(bvdid_final, 1, 2) == "US", 0, 1)) %>%
  filter(mnc == 1)
us_comms_mnc <- us_comms %>% filter(usmca == 1)%>%
  mutate(mnc = ifelse(substr(bvdid_final, 1, 2) == "US", 0, 1)) %>%
  filter(mnc == 1)
tab_a3$`Reports (Share)`[1] <- paste0(sum(parl_us_mnc$pres), " (", round(sum(parl_us_mnc$pres, na.rm = TRUE)/dim(us_comms_mnc[!is.na(us_comms_mnc$bvdid_final),])[1], 2), ")")
tab_a3$`Reports (Share)`[2] <- paste0(sum(parl_us_mnc$house), " (", round(sum(parl_us_mnc$house, na.rm = TRUE)/dim(us_comms_mnc[!is.na(us_comms_mnc$bvdid_final),])[1], 2), ")")
tab_a3$`Reports (Share)`[3] <- paste0(sum(parl_us_mnc$senate), " (", round(sum(parl_us_mnc$senate, na.rm = TRUE)/dim(us_comms_mnc[!is.na(us_comms_mnc$bvdid_final),])[1], 2), ")")
tab_a3$`Reports (Share)`[4] <- paste0(sum(parl_us_mnc$ustr), " (", round(sum(parl_us_mnc$ustr, na.rm = TRUE)/dim(us_comms_mnc[!is.na(us_comms_mnc$bvdid_final),])[1], 2), ")")
tab_a3$`Reports (Share)`[5] <- paste0(sum(parl_us_mnc$doc), " (", round(sum(parl_us_mnc$doc, na.rm = TRUE)/dim(us_comms_mnc[!is.na(us_comms_mnc$bvdid_final),])[1], 2), ")")
tab_a3$`Reports (Share)`[6] <- paste0(sum(parl_us_mnc$dag), " (", round(sum(parl_us_mnc$dag, na.rm = TRUE)/dim(us_comms_mnc[!is.na(us_comms_mnc$bvdid_final),])[1], 2), ")")
tab_a3$`Reports (Share)`[7] <- dim(us_comms_mnc[!is.na(us_comms_mnc$bvdid_final),])[1]

# rename cols
colnames(tab_a3) <- c("Institution", "Reports (Share)", "Clients (Share)")

# output table
options(knitr.kable.NA = '')
kableExtra::kable(tab_a3, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  caption = "Types of Insitutions lobbied in the United States, Non-US MNCs, 2016-2020", label = "tab_a3") %>%
  footnote(general = "The table shows the types of institutions lobbied in Canada during the USMCA negotiations by Non-US MNCs. This refers to the clients that report having lobbied or intended to lobby the respective governement institutions.", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c("hold_position"), font_size = 11) %>%
  add_header_above(c("", "United States USMCA Lobbying" = 2), bold = TRUE) %>%
  kableExtra::column_spec(1, width = "18em") %>%
  save_kable(file = "../output/table_a3.tex")


#### Table A4 ####

# Create Empty Shell DF
tab_a4 <- data.frame(`Institution` = c("President", "House",
                                        "Senate", "Trade Representative" , "Department of Commerce",
                                        "Department of Agriculture", " United States Total"),
                      `Share_Institutions` = rep(NA, 7),
                      `Unique_Clients_USMCA` = rep(NA,7),
                      stringsAsFactors = FALSE)

# Overall Client share of Institutions, Any agreemment
colnames(tab_a4) <- c("Institution", "Reports (Share)", "USMCA (Share)")

# Firm-level lobbying of Institutions, USMCA
us_usmca <- parl_us %>% filter(usmca == 1 & bvdid_final %in% clients_both$bvdid_final)
tab_a4$`USMCA (Share)`[1] <- paste0(sum(us_usmca$b_pres), " (", round(sum(us_usmca$b_pres)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a4$`USMCA (Share)`[2] <- paste0(sum(us_usmca$b_house), " (", round(sum(us_usmca$b_house)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a4$`USMCA (Share)`[3] <- paste0(sum(us_usmca$b_senate), " (", round(sum(us_usmca$b_senate)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a4$`USMCA (Share)`[4] <- paste0(sum(us_usmca$b_ustr), " (", round(sum(us_usmca$b_ustr)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a4$`USMCA (Share)`[5] <- paste0(sum(us_usmca$b_doc), " (", round(sum(us_usmca$b_doc)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a4$`USMCA (Share)`[6] <- paste0(sum(us_usmca$b_dag), " (", round(sum(us_usmca$b_dag)/length(unique(us_usmca$bvdid_final)), digits = 2), ")")
tab_a4$`USMCA (Share)`[7] <- length(unique(us_usmca$bvdid_final))

# How often does which institution get mentioned (out of all reports)
comms_us <- us_comms %>% filter(usmca == 1 & bvdid_final %in% clients_both$bvdid_final)
parl_us <- parl_us %>% filter(usmca == 1 & bvdid_final %in% clients_both$bvdid_final)
tab_a4$`Reports (Share)`[1] <- paste0(sum(parl_us$pres), " (", round(sum(parl_us$pres, na.rm = TRUE)/dim(comms_us[!is.na(comms_us$bvdid_final),])[1], 2), ")")
tab_a4$`Reports (Share)`[2] <- paste0(sum(parl_us$house), " (", round(sum(parl_us$house, na.rm = TRUE)/dim(comms_us[!is.na(comms_us$bvdid_final),])[1], 2), ")")
tab_a4$`Reports (Share)`[3] <- paste0(sum(parl_us$senate), " (", round(sum(parl_us$senate, na.rm = TRUE)/dim(comms_us[!is.na(comms_us$bvdid_final),])[1], 2), ")")
tab_a4$`Reports (Share)`[4] <- paste0(sum(parl_us$ustr), " (", round(sum(parl_us$ustr, na.rm = TRUE)/dim(comms_us[!is.na(comms_us$bvdid_final),])[1], 2), ")")
tab_a4$`Reports (Share)`[5] <- paste0(sum(parl_us$doc), " (", round(sum(parl_us$doc, na.rm = TRUE)/dim(comms_us[!is.na(comms_us$bvdid_final),])[1], 2), ")")
tab_a4$`Reports (Share)`[6] <- paste0(sum(parl_us$dag), " (", round(sum(parl_us$dag, na.rm = TRUE)/dim(comms_us[!is.na(comms_us$bvdid_final),])[1], 2), ")")
tab_a4$`Reports (Share)`[7] <- dim(comms_us[!is.na(comms_us$bvdid_final),])[1]


colnames(tab_a4) <- c("Institution", "Reports (Share)", "Clients (Share)")

options(knitr.kable.NA = '')
kableExtra::kable(tab_a4, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  caption = "Types of Insitutions lobbied in the United States, Clients Lobbying in both US and Canada", label = "tab_a4") %>%
  footnote(general = "The table shows the types of institutions lobbied in Canada during the USMCA negotiations by clients lobbying in both jurisdictions. This refers to the clients that report having lobbied or intended to lobby the respective governement institutions.", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c("hold_position"), font_size = 11) %>%
  add_header_above(c("", "United States USMCA Lobbying" = 2), bold = TRUE) %>%
  kableExtra::column_spec(1, width = "18em") %>%
  save_kable(file = "../output/table_a4.tex")


#### Table A5 ####

# read lobbying of institutions data
ca_clients_inst <- readr::read_csv("C:/Users/js.egb/Dropbox/Uni_Projects/us_can_nafta_lobby/submission/CPS/replication_material/data/can_clients_inst.csv")


# Create Empty Shell DF
tab_a5 <- data.frame(`Institution` = c("Prime Minister", "House of Commons",
                                        "Senate of Canada", "Global Affairs Canada" , "Agriculture & Agri-Food Canada",
                                        "Innov., Science, & Econ. Dev. Canada", "Finance Canada", "Canada Total"),
                      `Share_Institutions` = rep(NA, 8),
                      `Unique_Clients_USMCA` = rep(NA, 8),
                      stringsAsFactors = FALSE)


# Overall Client share of Institutions, Any agreemment
colnames(tab_a5) <- c("Institution", "Reports (Share)", "USMCA (Share)")

# Firm-level lobbying of Institutions, USMCA
# Note that "Global Affairs Canada", or "$trade" is includes 3 insitutions which changed the name over time:
# "Global Affairs Canada (GAC)"
# "Foreign Affairs, Trade and Development Canada"
# "Foreign Affairs and International Trade Canada (DFAITC)"
ca_usmca <- ca_clients_inst %>% 
  filter(bvdid_final %in% unique(ca_2016_2020$bvdid_final[ca_2016_2020$usmca==1]))
tab_a5$`USMCA (Share)`[1] <- paste0(sum(ca_usmca$b_pm), " (", round(sum(ca_usmca$b_pm)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a5$`USMCA (Share)`[2] <- paste0(sum(ca_usmca$b_house), " (", round(sum(ca_usmca$b_house)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a5$`USMCA (Share)`[3] <- paste0(sum(ca_usmca$b_senate), " (", round(sum(ca_usmca$b_senate)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a5$`USMCA (Share)`[4] <- paste0(sum(ca_usmca$b_trade), " (", round(sum(ca_usmca$b_trade)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a5$`USMCA (Share)`[5] <- paste0(sum(ca_usmca$b_aafc), " (", round(sum(ca_usmca$b_aafc)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a5$`USMCA (Share)`[6] <- paste0(sum(ca_usmca$b_ised), " (", round(sum(ca_usmca$b_ised)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a5$`USMCA (Share)`[7] <- paste0(sum(ca_usmca$b_fin), " (", round(sum(ca_usmca$b_fin)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a5$`USMCA (Share)`[8] <- length(unique(ca_usmca$bvdid_final))

# How often does which institution get mentioned (out of all reports)
ca_comms <- ca_clients_inst %>% 
  filter(bvdid_final %in% unique(ca_2016_2020$bvdid_final[ca_2016_2020$usmca==1]))
tab_a5$`Reports (Share)`[1] <- paste0(sum(ca_usmca$pm), " (", round(sum(ca_usmca$pm, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a5$`Reports (Share)`[2] <- paste0(sum(ca_usmca$house), " (", round(sum(ca_usmca$house, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a5$`Reports (Share)`[3] <- paste0(sum(ca_usmca$senate), " (", round(sum(ca_usmca$senate, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a5$`Reports (Share)`[4] <- paste0(sum(ca_usmca$trade,na.rm = TRUE), " (", round(sum(ca_usmca$trade, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a5$`Reports (Share)`[5] <- paste0(sum(ca_usmca$aafc), " (", round(sum(ca_usmca$aafc, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a5$`Reports (Share)`[6] <- paste0(sum(ca_usmca$ised), " (", round(sum(ca_usmca$ised, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a5$`Reports (Share)`[7] <- paste0(sum(ca_usmca$fin), " (", round(sum(ca_usmca$fin, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a5$`Reports (Share)`[8] <- sum(ca_comms$n_regs)

# rename cols
colnames(tab_a5) <- c("Institution", "Reports (Share)", "Clients (Share)")

options(knitr.kable.NA = '')
kableExtra::kable(tab_a5, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  caption = "Types of Insitutions lobbied in Canada, 2016-2020", label = "tab_a5") %>%
  footnote(general = "The table shows the types of institutions lobbied in the Canada during the USMCA negotiations. This refers to the clients that report having lobbied or intended to lobby the respective governement institutions.", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c("hold_position"), font_size = 11) %>%
  add_header_above(c("", "Canadian USMCA Lobbying" = 2), bold = TRUE) %>%
  kableExtra::column_spec(1, width = "18em") %>%
  save_kable(file = "../output/table_a5.tex")

#### Table A6 ####

# MNCs Create Empty Shell DF
tab_a6 <- data.frame(`Institution` = c("Prime Minister", "House of Commons",
                                            "Senate of Canada", "Global Affairs Canada" , "Agriculture & Agri-Food Canada",
                                            "Innov., Science, & Econ. Dev. Canada", "Finance Canada", "Canada Total"),
                          `Share_Institutions` = rep(NA, 8),
                          `Unique_Clients_USMCA` = rep(NA, 8),
                          stringsAsFactors = FALSE)


# Overall Client share of Institutions, Any agreemment
colnames(tab_a6) <- c("Institution", "Reports (Share)", "USMCA (Share)")

# Firm-level lobbying of Institutions, USMCA
# Note that "Global Affairs Canada", or "$trade" is includes 3 insitutions which changed the name over time:
# "Global Affairs Canada (GAC)"
# "Foreign Affairs, Trade and Development Canada"
# "Foreign Affairs and International Trade Canada (DFAITC)"
ca_usmca_mnc <- ca_clients_inst %>% 
  filter(bvdid_final %in% unique(ca_2016_2020$bvdid_final[ca_2016_2020$usmca==1])) %>%
  mutate(mnc = ifelse(substr(bvdid_final, 1, 2) == "CA", 0, 1)) %>%
  filter(mnc == 1)
tab_a6$`USMCA (Share)`[1] <- paste0(sum(ca_usmca_mnc$b_pm), " (", round(sum(ca_usmca_mnc$b_pm)/length(unique(ca_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a6$`USMCA (Share)`[2] <- paste0(sum(ca_usmca_mnc$b_house), " (", round(sum(ca_usmca_mnc$b_house)/length(unique(ca_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a6$`USMCA (Share)`[3] <- paste0(sum(ca_usmca_mnc$b_senate), " (", round(sum(ca_usmca_mnc$b_senate)/length(unique(ca_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a6$`USMCA (Share)`[4] <- paste0(sum(ca_usmca_mnc$b_trade), " (", round(sum(ca_usmca_mnc$b_trade)/length(unique(ca_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a6$`USMCA (Share)`[5] <- paste0(sum(ca_usmca_mnc$b_aafc), " (", round(sum(ca_usmca_mnc$b_aafc)/length(unique(ca_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a6$`USMCA (Share)`[6] <- paste0(sum(ca_usmca_mnc$b_ised), " (", round(sum(ca_usmca_mnc$b_ised)/length(unique(ca_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a6$`USMCA (Share)`[7] <- paste0(sum(ca_usmca_mnc$b_fin), " (", round(sum(ca_usmca_mnc$b_fin)/length(unique(ca_usmca_mnc$bvdid_final)), digits = 2), ")")
tab_a6$`USMCA (Share)`[8] <- length(unique(ca_usmca_mnc$bvdid_final))

# How often does which institution get mentioned (out of all reports)
ca_comms_mnc <- ca_clients_inst %>% 
  filter(bvdid_final %in% unique(ca_2016_2020$bvdid_final[ca_2016_2020$usmca==1])) %>%
  mutate(mnc = ifelse(substr(bvdid_final, 1, 2) == "CA", 0, 1)) %>%
  filter(mnc == 1)
tab_a6$`Reports (Share)`[1] <- paste0(sum(ca_usmca_mnc$pm), " (", round(sum(ca_usmca_mnc$pm, na.rm = TRUE)/sum(ca_comms_mnc$n_regs), 2), ")")
tab_a6$`Reports (Share)`[2] <- paste0(sum(ca_usmca_mnc$house), " (", round(sum(ca_usmca_mnc$house, na.rm = TRUE)/sum(ca_comms_mnc$n_regs), 2), ")")
tab_a6$`Reports (Share)`[3] <- paste0(sum(ca_usmca_mnc$senate), " (", round(sum(ca_usmca_mnc$senate, na.rm = TRUE)/sum(ca_comms_mnc$n_regs), 2), ")")
tab_a6$`Reports (Share)`[4] <- paste0(sum(ca_usmca_mnc$trade,na.rm = TRUE), " (", round(sum(ca_usmca_mnc$trade, na.rm = TRUE)/sum(ca_comms_mnc$n_regs), 2), ")")
tab_a6$`Reports (Share)`[5] <- paste0(sum(ca_usmca_mnc$aafc), " (", round(sum(ca_usmca_mnc$aafc, na.rm = TRUE)/sum(ca_comms_mnc$n_regs), 2), ")")
tab_a6$`Reports (Share)`[6] <- paste0(sum(ca_usmca_mnc$ised), " (", round(sum(ca_usmca_mnc$ised, na.rm = TRUE)/sum(ca_comms_mnc$n_regs), 2), ")")
tab_a6$`Reports (Share)`[7] <- paste0(sum(ca_usmca_mnc$fin), " (", round(sum(ca_usmca_mnc$fin, na.rm = TRUE)/sum(ca_comms_mnc$n_regs), 2), ")")
tab_a6$`Reports (Share)`[8] <- sum(ca_comms_mnc$n_regs)

# rename cols
colnames(tab_a6) <- c("Institution", "Reports (Share)", "Clients (Share)")

# output table
options(knitr.kable.NA = '')
kableExtra::kable(tab_a6, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  caption = "Types of Insitutions lobbied in Canada, Non-Canadian MNCs, 2016-2020", label = "tab_a6") %>%
  footnote(general = "The table shows the types of institutions lobbied in the Canada during the USMCA negotiations  by Non-Canadian MNCs. This refers to the clients that report having lobbied or intended to lobby the respective governement institutions.", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c("hold_position"), font_size = 11) %>%
  add_header_above(c("", "Canadian USMCA Lobbying" = 2), bold = TRUE) %>%
  kableExtra::column_spec(1, width = "18em") %>%
  save_kable(file = "../output/table_a6.tex")

#### Table A7 #####
# Create Empty Shell DF
tab_a7 <- data.frame(`Institution` = c("Prime Minister", "House of Commons",
                                        "Senate of Canada", "Global Affairs Canada" , "Agriculture & Agri-Food Canada",
                                        "Innov., Science, & Econ. Dev. Canada", "Finance Canada", "Canada Total"),
                      `Share_Institutions` = rep(NA, 8),
                      `Unique_Clients_USMCA` = rep(NA, 8),
                      stringsAsFactors = FALSE)


# Overall Client share of Institutions, Any agreemment
colnames(tab_a7) <- c("Institution", "Reports (Share)", "USMCA (Share)")

# Firm-level lobbying of Institutions, USMCA
# Note that "Global Affairs Canada", or "$trade" is includes 3 insitutions which changed the name over time:
# "Global Affairs Canada (GAC)"
# "Foreign Affairs, Trade and Development Canada"
# "Foreign Affairs and International Trade Canada (DFAITC)"
ca_usmca <- ca_clients_inst %>% filter(usmca == 1 & bvdid_final %in% clients_both$bvdid_final) 
tab_a7$`USMCA (Share)`[1] <- paste0(sum(ca_usmca$b_pm), " (", round(sum(ca_usmca$b_pm)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a7$`USMCA (Share)`[2] <- paste0(sum(ca_usmca$b_house), " (", round(sum(ca_usmca$b_house)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a7$`USMCA (Share)`[3] <- paste0(sum(ca_usmca$b_senate), " (", round(sum(ca_usmca$b_senate)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a7$`USMCA (Share)`[4] <- paste0(sum(ca_usmca$b_trade), " (", round(sum(ca_usmca$b_trade)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a7$`USMCA (Share)`[5] <- paste0(sum(ca_usmca$b_aafc), " (", round(sum(ca_usmca$b_aafc)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a7$`USMCA (Share)`[6] <- paste0(sum(ca_usmca$b_ised), " (", round(sum(ca_usmca$b_ised)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a7$`USMCA (Share)`[7] <- paste0(sum(ca_usmca$b_fin), " (", round(sum(ca_usmca$b_fin)/length(unique(ca_usmca$bvdid_final)), digits = 2), ")")
tab_a7$`USMCA (Share)`[8] <- length(unique(ca_usmca$bvdid_final))

# How often does which institution get mentioned (out of all reports)
ca_comms <- ca_clients_inst %>% filter(usmca == 1 & bvdid_final %in% clients_both$bvdid_final)
tab_a7$`Reports (Share)`[1] <- paste0(sum(ca_usmca$pm), " (", round(sum(ca_usmca$pm, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a7$`Reports (Share)`[2] <- paste0(sum(ca_usmca$house), " (", round(sum(ca_usmca$house, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a7$`Reports (Share)`[3] <- paste0(sum(ca_usmca$senate), " (", round(sum(ca_usmca$senate, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a7$`Reports (Share)`[4] <- paste0(sum(ca_usmca$trade,na.rm = TRUE), " (", round(sum(ca_usmca$trade, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a7$`Reports (Share)`[5] <- paste0(sum(ca_usmca$aafc), " (", round(sum(ca_usmca$aafc, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a7$`Reports (Share)`[6] <- paste0(sum(ca_usmca$ised), " (", round(sum(ca_usmca$ised, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a7$`Reports (Share)`[7] <- paste0(sum(ca_usmca$fin), " (", round(sum(ca_usmca$fin, na.rm = TRUE)/sum(ca_comms$n_regs), 2), ")")
tab_a7$`Reports (Share)`[8] <- sum(ca_comms$n_regs)

# rename cols
colnames(tab_a7) <- c("Institution", "Reports (Share)", "Clients (Share)")

# output table
options(knitr.kable.NA = '')
kableExtra::kable(tab_a7, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  caption = "Types of Insitutions lobbied in the Canada, Clients Lobbying in both US and Canada", label = "tab_a7") %>%
  footnote(general = "The table shows the types of institutions lobbied in Canada during the USMCA negotiations by clients lobbying in both jurisdictions. This refers to the clients that report having lobbied or intended to lobby the respective governement institutions.", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c("hold_position"), font_size = 11) %>%
  add_header_above(c("", "United States USMCA Lobbying" = 2), bold = TRUE) %>%
  kableExtra::column_spec(1, width = "18em") %>%
  save_kable(file = "../output/table_a7.tex")


#### Table A8 #####

# CAN read lobbying of institutions data
ca_clients_inst <- readr::read_csv("C:/Users/js.egb/Dropbox/Uni_Projects/us_can_nafta_lobby/submission/CPS/replication_material/data/can_clients_inst.csv")

# USA, read lobbying of institutions data
us_comms <- readr::read_csv("C:/Users/js.egb/Dropbox/Uni_Projects/us_can_nafta_lobby/submission/CPS/replication_material/data/usa_clients_comms.csv")

# aggregate at the client level, subet to 2016 to 2020
parl_us <- us_comms %>%
  # filter(report_year > = 2012) %>%
  filter(!is.na(bvdid_final), usmca == 1) %>%
  group_by(bvdid_final, orbis_name_final) %>%
  summarise(pres = sum(c(whouse+execpr), na.rm = TRUE),
            house = sum(house, na.rm = TRUE),
            senate = sum(senate, na.rm = TRUE),
            ustr = sum(ustr, na.rm = TRUE),
            doc = sum(doc, na.rm = TRUE ),
            dag = sum(dag, na.rm = TRUE),
            inst = sum(n_gov_entities, na.rm = TRUE),
            b_pres = ifelse(sum(c(whouse+execpr), na.rm = TRUE) > 0, 1, 0),
            b_house = ifelse(sum(house, na.rm = TRUE) > 0, 1, 0),
            b_senate = ifelse(sum(senate, na.rm = TRUE) > 0, 1, 0),
            b_ustr = ifelse(sum(ustr, na.rm = TRUE) > 0, 1, 0),
            b_doc = ifelse(sum(doc, na.rm = TRUE ) > 0, 1, 0),
            b_dag = ifelse(sum(dag, na.rm = TRUE) > 0, 1, 0),
            usmca = ifelse(sum(usmca, na.rm = TRUE) > 0, 1, 0),
            s_pres = sum(c(whouse+execpr), na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_house = sum(house, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_senate = sum(senate, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_ustr = sum(ustr, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_doc = sum(doc, na.rm = TRUE )/sum(n_gov_entities, na.rm = TRUE),
            s_dag = sum(dag, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE)) %>%
  filter(!is.na(bvdid_final)) %>%
  filter(bvdid_final %in% unique(us_2016_2020$bvdid_final[us_2016_2020$usmca==1]))

# CAN, USMCA clients, 2016-2020
ca_usmca <- ca_clients_inst %>% 
  filter(bvdid_final %in% unique(ca_2016_2020$bvdid_final[ca_2016_2020$usmca==1]))
# USA, USMCA clients, 2016-2020
us_usmca <- parl_us %>% filter(usmca == 1)



# First get firm-level binary outcomes
# CAN
fig2ca_df <- ca_usmca %>%
  mutate(country = "2CA") %>%
  group_by(bvdid_final, orbis_name_final, country) %>%
  mutate(pm_pres = ifelse(b_pm == 1, 1, 0),
         exe = ifelse(gaf > 0 | dfaitc > 0 | foraff > 0 | aafc > 0 | ised > 0 | fin > 0, 1, 0),
         house = ifelse(house > 0, 1, 0),
         senate = ifelse(senate > 0, 1, 0)) %>%
  select(bvdid_final, orbis_name_final, country, pm_pres, exe, house, senate)
# US
fig2us_df <- us_usmca %>%
  mutate(country = "1US") %>%
  group_by(bvdid_final, orbis_name_final, country) %>%
  mutate(pm_pres = ifelse(b_pres == 1, 1, 0),
         exe = ifelse(ustr > 0 | doc > 0 | dag > 0, 1, 0),
         house = ifelse(house > 0, 1, 0),
         senate = ifelse(senate > 0, 1, 0))%>%
  select(bvdid_final, orbis_name_final, country, pm_pres, exe, house, senate)

# rbind both DFs
fig2_df <- rbind(fig2ca_df, fig2us_df)

# run regressions for all 3 outcomes
lm_1 <- broom::tidy(summary(lm(pm_pres ~ factor(country), data = fig2_df)))
lm_2 <- broom::tidy(summary(lm(exe ~ factor(country), data = fig2_df)))
lm_3 <- broom::tidy(summary(lm(house ~ factor(country), data = fig2_df)))
lm_4 <- broom::tidy(summary(lm(senate ~ factor(country), data = fig2_df)))

# create empty DF for table
baltab <- data.frame(varname = c("President/PM", "Bureaucracy",
                                 "House", "Senate"),
                     n1 = rep(716, 4),
                     mean_1 = rep(NA, 4),
                     sd_1 = rep(NA, 4),
                     n2 = rep(138, 4),
                     mean_2 = rep(NA, 4),
                     sd_2 = rep(NA, 4),
                     diff = rep(NA, 4),
                     t_val = rep(NA, 4),
                     p_val = rep(NA, 4),
                     ci = rep(NA, 4))
# US
baltab$mean_1 <- round(apply(fig2_df[fig2_df$country=="1US",4:7], 2, mean), 2) # means
baltab$sd_1 <- round(apply(fig2_df[fig2_df$country=="1US",4:7], 2, sd), 2) # sd
# CA
baltab$mean_2 <- round(apply(fig2_df[fig2_df$country=="2CA",4:7], 2, mean), 2) # means
baltab$sd_2 <- round(apply(fig2_df[fig2_df$country=="2CA",4:7], 2, sd), 2) # sd
# Differences
baltab[1,c("diff","t_val","p_val")] <- round(lm_1[2,c("estimate","statistic","p.value")],2)
baltab[2,c("diff","t_val","p_val")] <- round(lm_2[2,c("estimate","statistic","p.value")],2)
baltab[3,c("diff","t_val","p_val")] <- round(lm_3[2,c("estimate","statistic","p.value")],2)
baltab[4,c("diff","t_val","p_val")] <- round(lm_4[2,c("estimate","statistic","p.value")],2)
# CIs
baltab$ci[1] <- paste0("(", round((baltab$diff[1]-lm_1[2,3]*1.96), 2), ", ", round((baltab$diff[1]+lm_1[2,3]*1.96), 2), ")")
baltab$ci[2] <- paste0("(", round((baltab$diff[2]-lm_2[2,3]*1.96), 2), ", ", round((baltab$diff[2]+lm_2[2,3]*1.96), 2), ")")
baltab$ci[3] <- paste0("(", round((baltab$diff[3]-lm_3[2,3]*1.96), 2), ", ", round((baltab$diff[3]+lm_3[2,3]*1.96), 2), ")")
baltab$ci[4] <- paste0("(", round((baltab$diff[4]-lm_4[2,3]*1.96), 2), ", ", round((baltab$diff[4]+lm_4[2,3]*1.96), 2), ")")
# p-values, all < 0.01
baltab$p_val <- rep("< 0.01", 4)

# add column names
colnames(baltab) <- c("Institution", "n", "Mean", "SD", "n", "Mean", "SD", "Diff.", "t-value", "p-value", "Conf.Int.")

# Function for Bold Column Names
bold <- function(x) {
  paste0('{\\bfseries ', x, '}')
}

# output to Table
library(kableExtra)
options(knitr.kable.NA = '')
kableExtra::kable(baltab, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  caption = "Insitutions lobbied in the United States and Canada, Differences, 2016-2020", label = "tab_a8") %>%
  footnote(general = "The table shows the mean differences in the shares of institutions lobbied in the United States and Canada, as in Figure 2 in the main text. In addition, it shows t-tests, p-values, and confidence intervals for the differences in means.", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c("hold_position"), font_size = 11) %>%
  add_header_above(c("", "United States" = 3, "Canada" = 3, "Mean Differences" = 4), bold = TRUE) %>%
  kableExtra::column_spec(1, width = "7em") %>%
  kableExtra::save_kable(file = "../output/table_a8.tex")



#### Table A9 ####

# US + Canadian lobbyisys-clients file
ca_lobbyists_clients <- readr::read_csv("C:/Users/js.egb/Dropbox/Uni_Projects/us_can_nafta_lobby/submission/CPS/replication_material/data/can_clients_lobbyists.csv")
us_lobbyists_clients <- readr::read_csv("C:/Users/js.egb/Dropbox/Uni_Projects/us_can_nafta_lobby/submission/CPS/replication_material/data/usa_clients_lobbyists.csv")


tab_a9 <- data.frame(`Org_Type` = c("Corporation", "Ideological Group", 
                                  "Peak Association", "Public" , "Trade Association", 
                                  "Union", "Total"),
                   `us_clients_ih` = rep(NA,7),
                   `us_clients_xt` = rep(NA,7),
                   `us_clients_both` = rep(NA,7),
                   `us_lobs_ih` = rep(NA,7),
                   `us_lobs_xt` = rep(NA,7),
                   `ca_clients_ih` = rep(NA,7),
                   `ca_clients_xt` = rep(NA,7),
                   `ca_clients_both` = rep(NA,7),
                   `ca_lobs_ih` = rep(NA,7),
                   `ca_lobs_xt` = rep(NA,7), stringsAsFactors = FALSE)
# US
us_clients <- us_lobbyists_clients %>% filter(usmca == 1)
tab_a9$us_clients_ih[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Corporation"]))
tab_a9$us_clients_ih[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Ideological Group"]))
tab_a9$us_clients_ih[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Peak Association"]))
tab_a9$us_clients_ih[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Public"]))
tab_a9$us_clients_ih[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Trade Association"]))
tab_a9$us_clients_ih[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Union"]))
tab_a9$us_clients_ih[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0]))

tab_a9$us_clients_xt[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Corporation"]))
tab_a9$us_clients_xt[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Ideological Group"]))
tab_a9$us_clients_xt[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Peak Association"]))
tab_a9$us_clients_xt[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Public"]))
tab_a9$us_clients_xt[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Trade Association"]))
tab_a9$us_clients_xt[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Union"]))
tab_a9$us_clients_xt[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1]))

tab_a9$us_clients_both[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Corporation"]))
tab_a9$us_clients_both[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Ideological Group"]))
tab_a9$us_clients_both[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Peak Association"]))
tab_a9$us_clients_both[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Public"]))
tab_a9$us_clients_both[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Trade Association"]))
tab_a9$us_clients_both[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Union"]))
tab_a9$us_clients_both[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1]))

tab_a9$us_lobs_ih[1] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Corporation"]),2)
tab_a9$us_lobs_ih[2] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Ideological Group"]),2)
tab_a9$us_lobs_ih[3] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Peak Association"]),2)
tab_a9$us_lobs_ih[4] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Public"]),2)
tab_a9$us_lobs_ih[5] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Trade Association"]),2)
tab_a9$us_lobs_ih[6] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Union"]),2)
tab_a9$us_lobs_ih[7] <- round(mean(us_clients$n_ihlob),2)

tab_a9$us_lobs_xt[1] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Corporation"]),2)
tab_a9$us_lobs_xt[2] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Ideological Group"]),2)
tab_a9$us_lobs_xt[3] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Peak Association"]),2)
tab_a9$us_lobs_xt[4] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Public"]),2)
tab_a9$us_lobs_xt[5] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Trade Association"]),2)
tab_a9$us_lobs_xt[6] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Union"]),2)
tab_a9$us_lobs_xt[7] <- round(mean(us_clients$n_xtlob),2)

# CAN
ca_clients <- ca_lobbyists_clients %>% filter(usmca == 1)
tab_a9$ca_clients_ih[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Corporation"]))
tab_a9$ca_clients_ih[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Ideological Group"]))
tab_a9$ca_clients_ih[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Peak Association"]))
tab_a9$ca_clients_ih[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Public"]))
tab_a9$ca_clients_ih[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Trade Association"]))
tab_a9$ca_clients_ih[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Union"]))
tab_a9$ca_clients_ih[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0]))

tab_a9$ca_clients_xt[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Corporation"]))
tab_a9$ca_clients_xt[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Ideological Group"]))
tab_a9$ca_clients_xt[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Peak Association"]))
tab_a9$ca_clients_xt[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Public"]))
tab_a9$ca_clients_xt[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Trade Association"]))
tab_a9$ca_clients_xt[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Union"]))
tab_a9$ca_clients_xt[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1]))

tab_a9$ca_clients_both[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Corporation"]))
tab_a9$ca_clients_both[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Ideological Group"]))
tab_a9$ca_clients_both[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Peak Association"]))
tab_a9$ca_clients_both[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Public"]))
tab_a9$ca_clients_both[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Trade Association"]))
tab_a9$ca_clients_both[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Union"]))
tab_a9$ca_clients_both[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1]))

tab_a9$ca_lobs_ih[1] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Corporation"]),2)
tab_a9$ca_lobs_ih[2] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Ideological Group"]),2)
tab_a9$ca_lobs_ih[3] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Peak Association"]),2)
tab_a9$ca_lobs_ih[4] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Public"]),2)
tab_a9$ca_lobs_ih[5] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Trade Association"]),2)
tab_a9$ca_lobs_ih[6] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Union"]),2)
tab_a9$ca_lobs_ih[7] <- round(mean(ca_clients$n_ihlob),2)

tab_a9$ca_lobs_xt[1] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Corporation"]),2)
tab_a9$ca_lobs_xt[2] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Ideological Group"]),2)
tab_a9$ca_lobs_xt[3] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Peak Association"]),2)
tab_a9$ca_lobs_xt[4] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Public"]),2)
tab_a9$ca_lobs_xt[5] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Trade Association"]),2)
tab_a9$ca_lobs_xt[6] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Union"]),2)
tab_a9$ca_lobs_xt[7] <- round(mean(ca_clients$n_xtlob),2)

# Print Table to LaTeX File
options(knitr.kable.NA = '')
kableExtra::kable(tab_a9, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  col.names = c("Organisation Type", rep(c("In House", "External", "Both", "In House", "External"), 2)), 
                  caption = "Types of Organisations Lobbying USMCA in the U.S. and Canada, Lobbyists Used", label = "tab2") %>%
  footnote(general = "The table shows the types of organizations lobbying on the USMCA in the U.S. in Canada, which types of lobbyists they use. First 3 columns: ``In House'' refers to clients who only use in house lobbyists, ``External'' refers to the those that only use external lobbyists (K-Street lobbyists). Last 2 columns: this refers to the average number of in house or external lobbyists", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c( "hold_position")) %>%
  add_header_above(c("", "# Clients Using Lobbyists" = 3, "Mean #Lobbyists" = 2, "# Clients Using Lobbyists" = 3, "Mean # Lobbyists" = 2), bold = TRUE) %>%
  add_header_above(c("", "United States" = 5, "Canada" = 5), bold = TRUE) %>%
  column_spec(1, width = "9em") %>%
  save_kable(file = "../output/table_a9.tex")



#### Table A10 ####
ca_lobbyists_clients <- readr::read_csv("C:/Users/js.egb/Dropbox/Uni_Projects/us_can_nafta_lobby/submission/CPS/replication_material/data/can_clients_lobbyists.csv")
us_lobbyists_clients <- readr::read_csv("C:/Users/js.egb/Dropbox/Uni_Projects/us_can_nafta_lobby/submission/CPS/replication_material/data/usa_clients_lobbyists.csv")

# INTERNAL/EXTERNAL LOBBYISTS by MNCs
tab_a10 <- data.frame(`Org_Type` = c("Corporation", "Ideological Group", 
                                  "Peak Association", "Public" , "Trade Association", 
                                  "Union", "Total"),
                   `us_clients_ih` = rep(NA,7),
                   `us_clients_xt` = rep(NA,7),
                   `us_clients_both` = rep(NA,7),
                   `us_lobs_ih` = rep(NA,7),
                   `us_lobs_xt` = rep(NA,7),
                   `ca_clients_ih` = rep(NA,7),
                   `ca_clients_xt` = rep(NA,7),
                   `ca_clients_both` = rep(NA,7),
                   `ca_lobs_ih` = rep(NA,7),
                   `ca_lobs_xt` = rep(NA,7), stringsAsFactors = FALSE)

# USA
us_clients <- us_lobbyists_clients %>% filter(usmca == 1) %>%
  mutate(mnc = ifelse(substr(bvdid_final, 1, 2) == "US", 0 , 1)) %>%
  filter(mnc == 1)
tab_a10$us_clients_ih[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Corporation"]))
tab_a10$us_clients_ih[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Ideological Group"]))
tab_a10$us_clients_ih[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Peak Association"]))
tab_a10$us_clients_ih[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Public"]))
tab_a10$us_clients_ih[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Trade Association"]))
tab_a10$us_clients_ih[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Union"]))
tab_a10$us_clients_ih[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0]))

tab_a10$us_clients_xt[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Corporation"]))
tab_a10$us_clients_xt[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Ideological Group"]))
tab_a10$us_clients_xt[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Peak Association"]))
tab_a10$us_clients_xt[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Public"]))
tab_a10$us_clients_xt[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Trade Association"]))
tab_a10$us_clients_xt[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Union"]))
tab_a10$us_clients_xt[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1]))

tab_a10$us_clients_both[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Corporation"]))
tab_a10$us_clients_both[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Ideological Group"]))
tab_a10$us_clients_both[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Peak Association"]))
tab_a10$us_clients_both[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Public"]))
tab_a10$us_clients_both[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Trade Association"]))
tab_a10$us_clients_both[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Union"]))
tab_a10$us_clients_both[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1]))

tab_a10$us_lobs_ih[1] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Corporation"]),2)
tab_a10$us_lobs_ih[2] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Ideological Group"]),2)
tab_a10$us_lobs_ih[3] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Peak Association"]),2)
tab_a10$us_lobs_ih[4] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Public"]),2)
tab_a10$us_lobs_ih[5] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Trade Association"]),2)
tab_a10$us_lobs_ih[6] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Union"]),2)
tab_a10$us_lobs_ih[7] <- round(mean(us_clients$n_ihlob),2)

tab_a10$us_lobs_xt[1] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Corporation"]),2)
tab_a10$us_lobs_xt[2] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Ideological Group"]),2)
tab_a10$us_lobs_xt[3] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Peak Association"]),2)
tab_a10$us_lobs_xt[4] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Public"]),2)
tab_a10$us_lobs_xt[5] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Trade Association"]),2)
tab_a10$us_lobs_xt[6] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Union"]),2)
tab_a10$us_lobs_xt[7] <- round(mean(us_clients$n_xtlob),2)

# CAN
ca_clients <- ca_lobbyists_clients %>% filter(usmca == 1) %>%
  mutate(mnc = ifelse(substr(bvdid_final, 1, 2) == "CA", 0 , 1)) %>%
  filter(mnc == 1) 
tab_a10$ca_clients_ih[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Corporation"]))
tab_a10$ca_clients_ih[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Ideological Group"]))
tab_a10$ca_clients_ih[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Peak Association"]))
tab_a10$ca_clients_ih[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Public"]))
tab_a10$ca_clients_ih[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Trade Association"]))
tab_a10$ca_clients_ih[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Union"]))
tab_a10$ca_clients_ih[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0]))

tab_a10$ca_clients_xt[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Corporation"]))
tab_a10$ca_clients_xt[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Ideological Group"]))
tab_a10$ca_clients_xt[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Peak Association"]))
tab_a10$ca_clients_xt[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Public"]))
tab_a10$ca_clients_xt[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Trade Association"]))
tab_a10$ca_clients_xt[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Union"]))
tab_a10$ca_clients_xt[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1]))

tab_a10$ca_clients_both[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Corporation"]))
tab_a10$ca_clients_both[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Ideological Group"]))
tab_a10$ca_clients_both[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Peak Association"]))
tab_a10$ca_clients_both[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Public"]))
tab_a10$ca_clients_both[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Trade Association"]))
tab_a10$ca_clients_both[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Union"]))
tab_a10$ca_clients_both[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1]))

tab_a10$ca_lobs_ih[1] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Corporation"]),2)
tab_a10$ca_lobs_ih[2] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Ideological Group"]),2)
tab_a10$ca_lobs_ih[3] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Peak Association"]),2)
tab_a10$ca_lobs_ih[4] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Public"]),2)
tab_a10$ca_lobs_ih[5] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Trade Association"]),2)
tab_a10$ca_lobs_ih[6] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Union"]),2)
tab_a10$ca_lobs_ih[7] <- round(mean(ca_clients$n_ihlob),2)

tab_a10$ca_lobs_xt[1] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Corporation"]),2)
tab_a10$ca_lobs_xt[2] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Ideological Group"]),2)
tab_a10$ca_lobs_xt[3] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Peak Association"]),2)
tab_a10$ca_lobs_xt[4] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Public"]),2)
tab_a10$ca_lobs_xt[5] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Trade Association"]),2)
tab_a10$ca_lobs_xt[6] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Union"]),2)
tab_a10$ca_lobs_xt[7] <- round(mean(ca_clients$n_xtlob),2)

# Print Table to LaTeX File
options(knitr.kable.NA = '')
kableExtra::kable(tab_a10, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  col.names = c("Organisation Type", rep(c("In House", "External", "Both", "In House", "External"), 2)), 
                  caption = "Types of Organisations Lobbying USMCA in the U.S. and Canada, Non-US/CAN MNCs, Lobbyists Used", label = "tab6") %>%
  footnote(general = "The table shows the types of organizations lobbying on the USMCA in the U.S. in Canada, which types of lobbyists they use. First 3 columns: ``In House'' refers to clients who only use in house lobbyists, ``External'' refers to the those that only use external lobbyists (K-Street lobbyists). Last 2 columns: this refers to the average number of in house or external lobbyists", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c( "hold_position")) %>%
  add_header_above(c("", "# Clients Using Lobbyists" = 3, "Mean #Lobbyists" = 2, "# Clients Using Lobbyists" = 3, "Mean # Lobbyists" = 2), bold = TRUE) %>%
  add_header_above(c("", "United States" = 5, "Canada" = 5), bold = TRUE) %>%
  column_spec(1, width = "9em") %>%
  save_kable(file = "../output/table_a10.tex")



#### Table A11 ####

# Subset by CA and US clients between 2016 and 2020
usa <- usa %>% filter(!is.na(bvdid_final)) %>%
  filter(bvdid_final %in% unique(us_2016_2020$bvdid_final[us_2016_2020$usmca==1]))
can <- can %>% filter(!is.na(bvdid_final)) %>%
  filter(bvdid_final %in% unique(ca_2016_2020$bvdid_final[ca_2016_2020$usmca==1]))

# create DF with clients lobbying in both countries
can$both <- ifelse(can$bvdid_final %in% usa$bvdid_final, 1, 0)
usa$both <- ifelse(usa$bvdid_final %in% can$bvdid_final, 1, 0) 

us_both_unique <- sort(unique(usa$bvdid_final[usa$both==1]))
ca_both_unique <- sort(unique(can$bvdid_final[can$both==1]))

clients_both <- can %>% filter(both == 1) %>%
  select(bvdid_final, org_type, orbis_name_final, GUO_Name, GUO_bvdid) %>%
  distinct()


# INTERNAL/EXTERNAL LOBBYISTS by those Clients lobbying in both Countries
tab_a11 <- data.frame(`Org_Type` = c("Corporation", "Ideological Group", 
                                  "Peak Association", "Public" , "Trade Association", 
                                  "Union", "Total"),
                   `us_clients_ih` = rep(NA,7),
                   `us_clients_xt` = rep(NA,7),
                   `us_clients_both` = rep(NA,7),
                   `us_lobs_ih` = rep(NA,7),
                   `us_lobs_xt` = rep(NA,7),
                   `ca_clients_ih` = rep(NA,7),
                   `ca_clients_xt` = rep(NA,7),
                   `ca_clients_both` = rep(NA,7),
                   `ca_lobs_ih` = rep(NA,7),
                   `ca_lobs_xt` = rep(NA,7), stringsAsFactors = FALSE)
# USA
us_clients <- us_lobbyists_clients %>% filter(usmca == 1 & bvdid_final %in% clients_both$bvdid_final)
tab_a11$us_clients_ih[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Corporation"]))
tab_a11$us_clients_ih[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Ideological Group"]))
tab_a11$us_clients_ih[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Peak Association"]))
tab_a11$us_clients_ih[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Public"]))
tab_a11$us_clients_ih[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Trade Association"]))
tab_a11$us_clients_ih[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0 & us_clients$org_type == "Union"]))
tab_a11$us_clients_ih[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==0]))

tab_a11$us_clients_xt[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Corporation"]))
tab_a11$us_clients_xt[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Ideological Group"]))
tab_a11$us_clients_xt[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Peak Association"]))
tab_a11$us_clients_xt[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Public"]))
tab_a11$us_clients_xt[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Trade Association"]))
tab_a11$us_clients_xt[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1 & us_clients$org_type == "Union"]))
tab_a11$us_clients_xt[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==0 & us_clients$xtlob==1]))

tab_a11$us_clients_both[1] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Corporation"]))
tab_a11$us_clients_both[2] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Ideological Group"]))
tab_a11$us_clients_both[3] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Peak Association"]))
tab_a11$us_clients_both[4] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Public"]))
tab_a11$us_clients_both[5] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Trade Association"]))
tab_a11$us_clients_both[6] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1 & us_clients$org_type == "Union"]))
tab_a11$us_clients_both[7] <- length(unique(us_clients$bvdid_final[us_clients$ihlob==1 & us_clients$xtlob==1]))

tab_a11$us_lobs_ih[1] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Corporation"]),2)
tab_a11$us_lobs_ih[2] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Ideological Group"]),2)
tab_a11$us_lobs_ih[3] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Peak Association"]),2)
tab_a11$us_lobs_ih[4] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Public"]),2)
tab_a11$us_lobs_ih[5] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Trade Association"]),2)
tab_a11$us_lobs_ih[6] <- round(mean(us_clients$n_ihlob[us_clients$org_type == "Union"]),2)
tab_a11$us_lobs_ih[7] <- round(mean(us_clients$n_ihlob),2)

tab_a11$us_lobs_xt[1] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Corporation"]),2)
tab_a11$us_lobs_xt[2] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Ideological Group"]),2)
tab_a11$us_lobs_xt[3] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Peak Association"]),2)
tab_a11$us_lobs_xt[4] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Public"]),2)
tab_a11$us_lobs_xt[5] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Trade Association"]),2)
tab_a11$us_lobs_xt[6] <- round(mean(us_clients$n_xtlob[us_clients$org_type == "Union"]),2)
tab_a11$us_lobs_xt[7] <- round(mean(us_clients$n_xtlob),2)

# CAN
ca_clients <- ca_lobbyists_clients %>% filter(usmca == 1 & bvdid_final %in% clients_both$bvdid_final)
tab_a11$ca_clients_ih[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Corporation"]))
tab_a11$ca_clients_ih[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Ideological Group"]))
tab_a11$ca_clients_ih[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Peak Association"]))
tab_a11$ca_clients_ih[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Public"]))
tab_a11$ca_clients_ih[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Trade Association"]))
tab_a11$ca_clients_ih[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0 & ca_clients$org_type == "Union"]))
tab_a11$ca_clients_ih[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==0]))

tab_a11$ca_clients_xt[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Corporation"]))
tab_a11$ca_clients_xt[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Ideological Group"]))
tab_a11$ca_clients_xt[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Peak Association"]))
tab_a11$ca_clients_xt[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Public"]))
tab_a11$ca_clients_xt[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Trade Association"]))
tab_a11$ca_clients_xt[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1 & ca_clients$org_type == "Union"]))
tab_a11$ca_clients_xt[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==0 & ca_clients$xtlob==1]))

tab_a11$ca_clients_both[1] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Corporation"]))
tab_a11$ca_clients_both[2] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Ideological Group"]))
tab_a11$ca_clients_both[3] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Peak Association"]))
tab_a11$ca_clients_both[4] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Public"]))
tab_a11$ca_clients_both[5] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Trade Association"]))
tab_a11$ca_clients_both[6] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1 & ca_clients$org_type == "Union"]))
tab_a11$ca_clients_both[7] <- length(unique(ca_clients$bvdid_final[ca_clients$ihlob==1 & ca_clients$xtlob==1]))

tab_a11$ca_lobs_ih[1] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Corporation"]),2)
tab_a11$ca_lobs_ih[2] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Ideological Group"]),2)
tab_a11$ca_lobs_ih[3] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Peak Association"]),2)
tab_a11$ca_lobs_ih[4] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Public"]),2)
tab_a11$ca_lobs_ih[5] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Trade Association"]),2)
tab_a11$ca_lobs_ih[6] <- round(mean(ca_clients$n_ihlob[ca_clients$org_type == "Union"]),2)
tab_a11$ca_lobs_ih[7] <- round(mean(ca_clients$n_ihlob),2)

tab_a11$ca_lobs_xt[1] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Corporation"]),2)
tab_a11$ca_lobs_xt[2] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Ideological Group"]),2)
tab_a11$ca_lobs_xt[3] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Peak Association"]),2)
tab_a11$ca_lobs_xt[4] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Public"]),2)
tab_a11$ca_lobs_xt[5] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Trade Association"]),2)
tab_a11$ca_lobs_xt[6] <- round(mean(ca_clients$n_xtlob[ca_clients$org_type == "Union"]),2)
tab_a11$ca_lobs_xt[7] <- round(mean(ca_clients$n_xtlob),2)


# Print Table to LaTeX File
library(knitr)
library(kableExtra)

options(knitr.kable.NA = '')
kableExtra::kable(tab_a11, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  col.names = c("Organisation Type", rep(c("In House", "External", "Both", "In House", "External"), 2)), 
                  caption = "Types of Organisations Lobbying USMCA in the U.S. and Canada, Clients Lobbying in Both Countries, Lobbyists Used", label = "tab5") %>%
  footnote(general = "The table shows the types of organizations lobbying on the USMCA in the U.S. in Canada, which types of lobbyists they use. First 3 columns: ``In House'' refers to clients who only use in house lobbyists, ``External'' refers to the those that only use external lobbyists (K-Street lobbyists). Last 2 columns: this refers to the average number of in house or external lobbyists", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c( "hold_position")) %>%
  add_header_above(c("", "# Clients Using Lobbyists" = 3, "Mean #Lobbyists" = 2, "# Clients Using Lobbyists" = 3, "Mean # Lobbyists" = 2), bold = TRUE) %>%
  add_header_above(c("", "United States" = 5, "Canada" = 5), bold = TRUE) %>%
  column_spec(1, width = "9em") %>%
  save_kable(file = "../output/table_a11.tex")




#### Figure A1 ####
# set wd to where code is, if using RStudio
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

us_lob_ts <- readr::read_csv("../data/panel_usa.csv")

# get top lobbying firms
us_lob_n <- us_lob_ts %>% group_by(bvdid_final) %>%
  summarise(lobby = sum(lobby, na.rm = TRUE)) %>%
  arrange(-lobby)

# Tile Plot of Firm/Association Lobbying Over Time
us_plot <- us_lob_ts %>%
  mutate(year = as.numeric(substr(year_q, 1, 4))) %>%
  group_by(bvdid_final) %>%
  filter(year %in% c(2016:2020)) %>%
  filter(sum(usmca)>0) %>%
  filter(bvdid_final %in% us_lob_n$bvdid_final[1:400])

scale <- 1.25
library(ggplot2)
tile_plot <- ggplot(us_plot, aes(x=year_q,y=reorder(orbis_name_final, lobby, sum), fill=as.factor(usmca)))+
  geom_tile(color= "white",size=0.1, show.legend = TRUE) +
  scale_fill_manual("U.S. Lobbying",
                    values = c("0" = "lightgrey", "1" = "#2E74C0"),
                    labels = c("No Activity", "Lobby USMCA")) +
  theme(panel.background = element_rect(fill = 'white')) +
  #labs(title= paste0("Energy Sector (NAICS 221)")) +
  ylab("Client") +
  xlab("\nQuarter\n") +
  theme_minimal(base_size = 5) +
  theme(plot.title=element_text(size = 10*scale))+
  theme(axis.title = element_text(color = "black", face = "bold", size = 12*scale))+
  theme(axis.text.x=element_text(color = "black", size=7*scale, angle = 45, hjust = 1)) +
  theme(axis.text.y=element_blank()) +
  theme(legend.title = element_text(color = "black", size = 10*scale, face = "bold"))+
  theme(legend.text = element_text(color = "black", size = 8*scale)) +
  theme(legend.position="top", legend.spacing.x = unit(1.0, 'cm'),
        legend.margin=margin(0,0,0,0), legend.box.margin=margin(20,20,20,20)) +
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(strip.background = element_rect(colour="white")) +
  scale_x_discrete(breaks = unique(us_plot$year_q))

tile_plot
ggsave(tile_plot, filename = "../output/fig_a1a.pdf", width = 6, height = 7)
dev.off()


# Plot Figure: w/o firm labels and square
ca_lob_ts <- readr::read_csv("../data/panel_can.csv")

ca_plot <- ca_lob_ts %>%
  group_by(bvdid_final) %>%
  filter(year %in% c(2016:2020)) %>%
  filter(sum(usmca) > 0)

scale <- 1.25
library(ggplot2)
tile_plot <- ggplot(ca_plot, aes(x=year_q_ch,y=reorder(orbis_name_final, lobby_usmca, sum), fill=as.factor(usmca)))+
  geom_tile(color= "white",size=0.1, show.legend = TRUE) +
  scale_fill_manual("Canadian Lobbying",
                    values = c("0" = "lightgrey", "1" = "#CB454A"),
                    labels = c("No Activity", "Lobby USMCA")) +
  theme(panel.background = element_rect(fill = 'white')) +
  #labs(title= paste0("Energy Sector (NAICS 221)")) +
  ylab("Client") +
  xlab("\nQuarter\n") +
  theme_minimal(base_size = 5) +
  theme(plot.title=element_text(size = 10*scale))+
  theme(axis.title = element_text(color = "black", face = "bold", size = 12*scale))+
  theme(axis.text.x=element_text(color = "black", size=7*scale, angle = 45, hjust = 1)) +
  theme(axis.text.y=element_blank()) +
  theme(legend.title = element_text(color = "black", size = 10*scale, face = "bold"))+
  theme(legend.text = element_text(color = "black", size = 8*scale)) +
  theme(legend.position="top", legend.spacing.x = unit(1.0, 'cm'),
        legend.margin=margin(0,0,0,0), legend.box.margin=margin(20,20,20,20)) +
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(strip.background = element_rect(colour="white")) +
  scale_x_discrete(breaks = unique(ca_plot$year_q_ch))

tile_plot
ggsave(tile_plot, filename = "../output/fig_a1b.pdf", width = 6, height = 7)
dev.off()



#### Figure A2 ####

# Tile Plot of Firm/Association Lobbying Over Time
us_plot <- us_lob_ts %>%
  mutate(year = as.numeric(substr(year_q, 1, 4))) %>%
  group_by(bvdid_final) %>%
  filter(year %in% c(2016:2020), lobby_usmca > 0) 

# get top lobbying firms
us_lob_n <- us_lob_ts %>% group_by(bvdid_final) %>%
  summarise(lobby = sum(lobby, na.rm = TRUE)) %>%
  arrange(-lobby)

scale <- 1.25
library(ggplot2)
tile_plot <- ggplot(us_plot[us_plot$bvdid_final %in% us_lob_n$bvdid_final[1:150],], aes(x=year_q,y=reorder(orbis_name_final, lobby, sum), fill=as.factor(usmca)))+
  geom_tile(color= "white",size=0.1, show.legend = TRUE) +
  scale_fill_manual("U.S. Lobbying",
                    values = c("0" = "lightgrey", "1" = "#2E74C0"),
                    labels = c("No Activity", "Lobby USMCA")) +
  theme(panel.background = element_rect(fill = 'white')) +
  #labs(title= paste0("Energy Sector (NAICS 221)")) +
  ylab("Client") +
  xlab("\nQuarter\n") +
  theme_minimal(base_size = 5) +
  theme(plot.title=element_text(size = 10*scale))+
  theme(axis.title = element_text(color = "black", face = "bold", size = 8*scale))+
  theme(axis.text.x=element_text(color = "black", size=5*scale, angle = 45, hjust = 1)) +
  theme(axis.text.y=element_text(size = 5*scale)) +
  theme(legend.title = element_text(color = "black", size = 10*scale, face = "bold"))+
  theme(legend.text = element_text(color = "black", size = 8*scale)) +
  theme(legend.position="top", legend.spacing.x = unit(1.0, 'cm'),
        legend.margin=margin(0,0,0,0), legend.box.margin=margin(20,20,20,20)) +
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(strip.background = element_rect(colour="white")) +
  scale_x_discrete(breaks = unique(us_plot$year_q))

tile_plot
ggsave(tile_plot, filename = "../output/fig_a2.pdf", width = 9, height = 12)
dev.off()




#### Figure A3 ####

# Plot Figure: With Firm Labels
scale <- 1.25
library(ggplot2)
tile_plot11 <- ggplot(ca_plot, aes(x=year_q_ch,y=reorder(orbis_name_final, lobby_usmca, sum), fill=as.factor(usmca)))+
  geom_tile(color= "white",size=0.1, show.legend = TRUE) +
  scale_fill_manual("Canadian Lobbying",
                    values = c("0" = "lightgrey", "1" = "#CB454A"),
                    labels = c("No Activity", "Lobby USMCA")) +
  theme(panel.background = element_rect(fill = 'white')) +
  #labs(title= paste0("Energy Sector (NAICS 221)")) +
  ylab("Client") +
  xlab("\nQuarter\n") +
  theme_minimal(base_size = 5) +
  theme(plot.title=element_text(size = 10*scale))+
  theme(axis.title = element_text(color = "black", face = "bold", size = 8*scale))+
  theme(axis.text.x=element_text(color = "black", size=5*scale, angle = 45, hjust = 1)) +
  theme(axis.text.y=element_text(size = 2*scale)) +
  theme(legend.title = element_text(color = "black", size = 10*scale, face = "bold"))+
  theme(legend.text = element_text(color = "black", size = 8*scale)) +
  theme(legend.position="top", legend.spacing.x = unit(1.0, 'cm'),
        legend.margin=margin(0,0,0,0), legend.box.margin=margin(20,20,20,20)) +
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(strip.background = element_rect(colour="white")) +
  scale_x_discrete(breaks = unique(ca_plot$year_q_ch))

tile_plot11
ggsave(filename = "../output/fig_a3.pdf", width = 9, height = 12)
dev.off()

# clean up
rm(ca_lob_ts, ca_plot, tile_plot, tile_plot11, us_lob_n, us_lob_ts, us_plot, scale)



#### Table B1 ####
# read in data
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
usa <- readr::read_csv("../data/usa_clients_tpp.csv")
can <- readr::read_csv("../data/can_clients_tpp.csv")


# aggregate
usa_count <- usa %>%
  select(bvdid_final, orbis_name_final, org_type) %>%
  distinct() %>% 
  mutate(ISO2 = substr(bvdid_final, 1, 2),
         tpp = ifelse(bvdid_final %in% unique(usa$bvdid_final[usa$tpp == 1]) , 1, 0),
         usmca = ifelse(bvdid_final %in% unique(usa$bvdid_final[usa$usmca == 1]), 1, 0))

# remove some duplicates
usa_count <- usa_count[-which(duplicated(usa_count$bvdid_final)),]



# For Descriptive Purposes
tab1_tpp <- data.frame(`Org_Type` = c("Corporation", "Ideological Group", 
                                      "Peak Association", "Public" , "Trade Association", 
                                      "Union", "Total"),
                       `all_TPP` = rep(NA,7),
                       `perc_TPP` = rep(NA,7),
                       `us_TPP` = rep(NA,7),
                       `us_perc` = rep(NA,7),
                       `ca_TPP` = rep(NA,7),
                       `ca_perc` = rep(NA,7),
                       `both_TPP` = rep(NA,7),
                       `both_perc` = rep(NA,7), stringsAsFactors = FALSE)
# USA
tab1_tpp$`us_TPP` <- c(table(usa_count$org_type[!is.na(usa_count$bvdid_final) & usa_count$tpp==1]), sum(table(usa_count$org_type[!is.na(usa_count$bvdid_final) & usa_count$tpp==1])))
tab1_tpp$us_perc[1:6] <- round(tab1_tpp$us_TPP[1:6]/tab1_tpp$us_TPP[7], 2)


# Canada
tab1_tpp$`ca_TPP`[1] <- length(unique(can$bvdid_final[can$org_type=="Corporation" & can$tpp==1]))
tab1_tpp$`ca_TPP`[2] <- length(unique(can$bvdid_final[can$org_type=="Ideological Group" & can$tpp==1]))
tab1_tpp$`ca_TPP`[3] <- length(unique(can$bvdid_final[can$org_type=="Peak Association" & can$tpp==1]))
tab1_tpp$`ca_TPP`[4] <- length(unique(can$bvdid_final[can$org_type=="Public" & can$tpp==1]))
tab1_tpp$`ca_TPP`[5] <- length(unique(can$bvdid_final[can$org_type=="Trade Association" & can$tpp==1]))
tab1_tpp$`ca_TPP`[6] <- length(unique(can$bvdid_final[can$org_type=="Union" & can$tpp==1]))
tab1_tpp$`ca_TPP`[7] <- length(unique(can$bvdid_final[can$tpp==1]))

tab1_tpp$ca_perc[1:6] <- round(tab1_tpp$ca_TPP[1:6]/tab1_tpp$ca_TPP[7], 2)

# Both Countries
tab1_tpp$`all_TPP`[1] <- length(unique(c(usa_count$bvdid_final[usa_count$org_type=="Corporation" & usa_count$tpp==1], can$bvdid_final[can$org_type=="Corporation" & can$tpp==1])))
tab1_tpp$`all_TPP`[2] <- length(unique(c(usa_count$bvdid_final[usa_count$org_type=="Ideological Group" & usa_count$tpp==1], can$bvdid_final[can$org_type=="Ideological Group" & can$tpp==1])))
tab1_tpp$`all_TPP`[3] <- length(unique(c(usa_count$bvdid_final[usa_count$org_type=="Peak Association" & usa_count$tpp==1], can$bvdid_final[can$org_type=="Peak Association" & can$tpp==1])))
tab1_tpp$`all_TPP`[4] <- length(unique(c(usa_count$bvdid_final[usa_count$org_type=="Public" & usa_count$tpp==1], can$bvdid_final[can$org_type=="Public" & can$tpp==1])))
tab1_tpp$`all_TPP`[5] <- length(unique(c(usa_count$bvdid_final[usa_count$org_type=="Trade Association" & usa_count$tpp==1], can$bvdid_final[can$org_type=="Trade Association" & can$tpp==1])))
tab1_tpp$`all_TPP`[6] <- length(unique(c(usa_count$bvdid_final[usa_count$org_type=="Union" & usa_count$tpp==1], can$bvdid_final[can$org_type=="Union" & can$tpp==1])))
tab1_tpp$`all_TPP`[7] <- length(unique(c(usa_count$bvdid_final[usa_count$tpp==1], can$bvdid_final[can$tpp==1])))

tab1_tpp$perc_TPP[1:6] <- round(tab1_tpp$all_TPP[1:6]/tab1_tpp$all_TPP[7], 2)


# Which clients in USA also lobby in Canada, which Canadian Clients also Lobby in the US?
can$lob_usa <- ifelse(can$bvdid_final %in% usa$bvdid_final, 1, 0)
can$lob_usa_tpp <- ifelse(can$bvdid_final %in% usa$bvdid_final[usa$tpp==1], 1, 0)
can$lob_usa_usmca <- ifelse(can$bvdid_final %in% usa$bvdid_final[usa$usmca==1], 1, 0)

usa$lob_can <- ifelse(usa$bvdid_final %in% can$bvdid_final, 1, 0)
usa$lob_can_tpp <- ifelse(usa$bvdid_final %in% can$bvdid_final[can$tpp==1], 1, 0)
usa$lob_can_usmca <- ifelse(usa$bvdid_final %in% can$bvdid_final[can$usmca==1], 1, 0)


# Table for both
tab1_tpp$both_TPP[1] <- sum(unique(usa$bvdid_final[usa$firm==1 & usa$tpp==1]) %in% unique(can$bvdid_final[can$firm==1 & can$tpp==1]))
tab1_tpp$both_TPP[2] <- sum(unique(usa$bvdid_final[usa$ideol_group==1 & usa$tpp==1]) %in% unique(can$bvdid_final[can$ideol_group==1 & can$tpp==1]))
tab1_tpp$both_TPP[3] <- sum(unique(usa$bvdid_final[usa$peak_assoc==1 & usa$tpp==1]) %in% unique(can$bvdid_final[can$peak_assoc==1 & can$tpp==1]))
tab1_tpp$both_TPP[4] <- sum(unique(usa$bvdid_final[usa$public==1 & usa$tpp==1]) %in% unique(can$bvdid_final[can$public==1 & can$tpp==1]))
tab1_tpp$both_TPP[5] <- sum(unique(usa$bvdid_final[usa$trade_assoc==1 & usa$tpp==1]) %in% unique(can$bvdid_final[can$trade_assoc==1 & can$tpp==1]))
tab1_tpp$both_TPP[6] <- sum(unique(usa$bvdid_final[usa$union==1 & usa$tpp==1]) %in% unique(can$bvdid_final[can$union==1 & can$tpp==1]))
tab1_tpp$both_TPP[7] <- sum(tab1_tpp$both_TPP[1:6])

tab1_tpp$both_perc[1:6] <- round(tab1_tpp$both_TPP[1:6]/tab1_tpp$both_TPP[7], 2)


# Print Table to LaTeX File
options(knitr.kable.NA = '')
kableExtra::kable(tab1_tpp, "latex", booktabs = T, digits = 2, linesep = "\\addlinespace",
                  col.names = c("Organisation Type", rep(c("Clients", "Share"), 4)), 
                  caption = "Types of Organisations Lobbying TPP in the U.S. (2012-2016) and Canada (2012-2020)", label = "tab_b1") %>%
  footnote(general = "The table shows the types of organizations lobbying on the TPP in the U.S. (2012-Q1 to 2020-Q2) and in Canada (2012-Q1 to 2020-Q2).", threeparttable = T) %>%
  kableExtra::kable_styling(latex_options = c( "hold_position")) %>%
  add_header_above(c("", "Overall" = 2, "in United States" = 2, "in Canada" = 2, "in Both" = 2), bold = TRUE) %>%
  add_header_above(c("", "Unique Clients Lobbying the TPP:" = 8), bold = TRUE) %>%
  column_spec(1, width = "9em") %>%
  save_kable(file = "../output/table_b1.tex")


#### Figure B1 ####
# USA
us_naics_plot <- readr::read_csv("../data/us_naics_count_tpp.csv")

naics_hist_us <- ggplot(us_naics_plot, aes(fill=type, y=value, x=reorder(naics3, -value))) + 
  geom_bar(position="stack", stat="identity") + 
  ylab("Unique 6-Digit NAICS Codes") +
  xlab("3-Digit NAICS Code") +
  scale_fill_manual(values = c("#999999", "#56B4E9", "#0072B2")) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle=45, vjust=3.75, hjust=2, size = 7, colour = "black"),
        axis.text.y = element_text(colour = "black", size = 15),
        legend.position = c(0.5, 0.9),
        legend.direction = "horizontal",
        legend.title = element_blank(),
        panel.grid = element_blank(),
        panel.grid.major.y = element_line(colour = "lightgrey"),
        axis.ticks.y = element_line(colour = "black",),
        legend.text = element_text(colour = "black", size = 16),
        axis.title.x = element_blank(),
        axis.title.y = element_text(colour = "black", face = "bold", size = 17)) 

ggsave(naics_hist_us, filename = "../output/fig_b1us.pdf", scale = 2.25)
sum(us_naics_plot$value) # 464 industries mobilizing in the US

# CAN
ca_naics_plot <- readr::read_csv("../data/ca_naics_count_tpp.csv")

naics_hist_ca <- ggplot(ca_naics_plot, aes(fill=type, y=value, x=reorder(naics3, -value))) + 
  geom_bar(position="stack", stat="identity") + 
  ylab("Unique 6-Digit NAICS Codes") +
  xlab("3-Digit NAICS Code") +
  ylim(0, 30) +
  scale_fill_manual(values = c("#999999", "#56B4E9", "#0072B2")) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle=45, vjust=3.5, hjust=2, size = 9, colour = "black"),
        axis.text.y = element_text(colour = "black", size = 15),
        #legend.position = c(0.5, 0.9),
        legend.position = "none",
        legend.direction = "horizontal",
        legend.title = element_blank(),
        panel.grid = element_blank(),
        panel.grid.major.y = element_line(colour = "lightgrey"),
        axis.ticks.y = element_line(colour = "black",),
        #legend.text = element_text(colour = "black", size = 16),
        axis.title.x = element_text(colour = "black", face = "bold", size = 17),
        axis.title.y = element_text(colour = "black", face = "bold", size = 17)) 

ggsave(naics_hist_ca, filename = "../output/fig_b1ca.pdf", scale = 2.25)
sum(ca_naics_plot$value) # 184 industries mobilizing in CA



#### Figure B2 ####
# USA
us_2016_2020 <- readr::read_csv("../data/panel_usa.csv") %>%
  filter(year_q %in% c("2012-Q1", "2012-Q2", "2012-Q3", "2012-Q4",
                       "2013-Q1", "2013-Q2", "2013-Q3", "2013-Q4",
                       "2014-Q1", "2014-Q2", "2014-Q3", "2014-Q4",
                       "2015-Q1", "2015-Q2", "2015-Q3", "2015-Q4",
                       "2016-Q1", "2016-Q2", "2016-Q3", "2016-Q4"))

# CAN
ca_2016_2020 <- readr::read_csv("../data/panel_can.csv") %>%
  filter(year_q_ch %in% c("2012-Q1", "2012-Q2", "2012-Q3", "2012-Q4",
                          "2013-Q1", "2013-Q2", "2013-Q3", "2013-Q4",
                          "2014-Q1", "2014-Q2", "2014-Q3", "2014-Q4",
                          "2015-Q1", "2015-Q2", "2015-Q3", "2015-Q4",
                          "2016-Q1", "2016-Q2", "2016-Q3", "2016-Q4",
                          "2017-Q1", "2017-Q2", "2017-Q3", "2017-Q4",
                          "2018-Q1", "2018-Q2", "2018-Q3", "2018-Q4",
                          "2019-Q1", "2019-Q2", "2019-Q3", "2019-Q4",
                          "2020-Q1", "2020-Q2"))

# CAN
ca_clients_inst <- readr::read_csv("../data/can_clients_inst.csv")
ca_tpp <- ca_clients_inst %>% 
  filter(bvdid_final %in% unique(ca_2016_2020$bvdid_final[ca_2016_2020$tpp==1]))

# USA
us_comms <- readr::read_csv("../data/usa_clients_comms.csv")

# aggregate at the client level, subet to 2016 to 2020
parl_us <- us_comms %>%
  # filter(report_year > = 2012) %>%
  filter(!is.na(bvdid_final), tpp == 1) %>%
  group_by(bvdid_final, orbis_name_final) %>%
  summarise(pres = sum(c(whouse+execpr), na.rm = TRUE),
            house = sum(house, na.rm = TRUE),
            senate = sum(senate, na.rm = TRUE),
            ustr = sum(ustr, na.rm = TRUE),
            doc = sum(doc, na.rm = TRUE ),
            dag = sum(dag, na.rm = TRUE),
            inst = sum(n_gov_entities, na.rm = TRUE),
            b_pres = ifelse(sum(c(whouse+execpr), na.rm = TRUE) > 0, 1, 0),
            b_house = ifelse(sum(house, na.rm = TRUE) > 0, 1, 0),
            b_senate = ifelse(sum(senate, na.rm = TRUE) > 0, 1, 0),
            b_ustr = ifelse(sum(ustr, na.rm = TRUE) > 0, 1, 0),
            b_doc = ifelse(sum(doc, na.rm = TRUE ) > 0, 1, 0),
            b_dag = ifelse(sum(dag, na.rm = TRUE) > 0, 1, 0),
            usmca = ifelse(sum(usmca, na.rm = TRUE) > 0, 1, 0),
            tpp = ifelse(sum(tpp, na.rm = TRUE) > 0, 1, 0),
            s_pres = sum(c(whouse+execpr), na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_house = sum(house, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_senate = sum(senate, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_ustr = sum(ustr, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE),
            s_doc = sum(doc, na.rm = TRUE )/sum(n_gov_entities, na.rm = TRUE),
            s_dag = sum(dag, na.rm = TRUE)/sum(n_gov_entities, na.rm = TRUE)) %>%
  filter(!is.na(bvdid_final)) %>%
  filter(bvdid_final %in% unique(us_2016_2020$bvdid_final[us_2016_2020$tpp==1]))

us_tpp <- parl_us %>% filter(tpp == 1)


fig3ca <- ca_tpp %>%
  mutate(country = "CA") %>%
  group_by(bvdid_final, orbis_name_final, country) %>%
  summarise(pm = ifelse(b_pm == 1, 1, 0),
            exe = ifelse(gaf > 0 | dfaitc > 0 | foraff > 0 | aafc > 0 | ised > 0 | fin > 0, 1, 0),
            house = ifelse(house > 0, 1, 0),
            senate = ifelse(senate > 0, 1, 0)) %>%
  ungroup() %>%
  summarise(pres = sum(pm)/n(),
            exe = sum(exe)/n(),
            house = sum(house)/n(),
            senate = sum(senate)/n())

fig3us <- us_tpp %>%
  group_by(bvdid_final, orbis_name_final) %>%
  summarise(pres = ifelse(b_pres == 1, 1, 0),
            exe = ifelse(ustr > 0 | doc > 0 | dag > 0, 1, 0),
            house = ifelse(house > 0, 1, 0),
            senate = ifelse(senate > 0, 1, 0)) %>%
  ungroup() %>%
  summarise(pres = sum(pres)/n(),
            exe = sum(exe)/n(),
            house = sum(house)/n(),
            senate = sum(senate)/n())



df <- data.frame(Country = rep(c("United States (n=527)", "Canada (n=178)"), each = 4),
                 Institution = rep(c("President\nor\nPrime Minister", "Bureaucracy", "House", "Senate"),  2),
                 Lobby = round(unlist(c(fig3us, fig3ca)), 2)) %>%
  mutate(Country = factor(Country, levels = c("United States (n=527)", "Canada (n=178)")),
         Institution = factor(Institution, levels = c("House", "Senate", "President\nor\nPrime Minister", "Bureaucracy")))

library(ggplot2)  
ggplot(df, aes(x = Institution, y = Lobby, fill = Country) ) +
  geom_col(position = position_dodge(0.8), width = 0.8) +
  scale_fill_grey() +
  theme_minimal() +
  xlab("\nInstitution Lobbied") +
  ylab("Share of Clients\n") +
  theme(axis.text = element_text(face = "plain", colour = "black", size = 16)) +
  theme(axis.title = element_text(face = "bold", colour = "black", size = 19)) +
  theme(legend.position="top") +
  theme(legend.title = element_blank()) +
  theme(legend.text =  element_text(face = "plain", colour = "black", size = 17)) 

ggsave(filename = "../output/fig_b2.pdf", width = 9, height = 6.5)
dev.off()



#### Figure B3 #### 
ca_lobbyists_clients <- readr::read_csv("../data/can_clients_lobbyists_tpp.csv")
us_lobbyists_clients <- readr::read_csv("../data/usa_clients_lobbyists_tpp.csv")

# subset to TPP
ca_clients <- ca_lobbyists_clients %>% filter(tpp == 1)
us_clients <- us_lobbyists_clients %>% filter(tpp == 1)

# Density Plots for usage of Lobbyists
us_clients <- us_clients[,c(names(ca_clients))] # keep same variables
us_clients$ID <- 1:dim(us_clients)[1] # indicator for country and new ID
ca_clients$ID <- c((dim(us_clients)[1]+1):(dim(us_clients)[1]+dim(ca_clients)[1])) # indicator for country and new ID
us_clients$country <- "United States" # indicator for US/CA
ca_clients$country <- "Canada"
clients <- dplyr::bind_rows(us_clients, ca_clients) %>% # rbind data
  mutate(n_lob = n_xtlob + n_ihlob)


# All Lobbyists
dens_lobs_all <- ggplot(clients, aes(x=n_lob, fill=country)) +
  geom_density(alpha = 0.4, adjust = 0.7, size = 0, color = NA) +
  theme_bw() +
  scale_x_log10(breaks=c(0, 1, 10,100, 1000), limits = c(0.5,300)) +
  scale_y_continuous(breaks=c(seq(0,1.25, 0.25)), limits = c(0,1.25)) +
  scale_fill_manual("Country", 
                    values = c(gray.colors(3, 0.2, 0.6)[3],gray.colors(3, 0.2, 0.6)[1]),
                    labels = c("Canada", "United States")) +
  geom_vline(xintercept = mean(clients$n_lob[clients$country=="Canada"]), linetype = "dashed", color = gray.colors(3, 0.2, 0.6)[3], size = 1) +
  geom_vline(xintercept = mean(clients$n_lob[clients$country=="United States"]), linetype = "dashed", color = gray.colors(3, 0.2, 0.6)[1], size = 1) +
  annotate("text", x=mean(clients$n_lob[clients$country=="Canada"])-2.25, y=1, label=paste0(round(mean(clients$n_lob[clients$country=="Canada"]), 1)), size=6, angle = 0, color = gray.colors(3, 0.2, 0.6)[2]) +
  annotate("text", x=mean(clients$n_lob[clients$country=="United States"])+5.1, y=1, label=paste0(round(mean(clients$n_lob[clients$country=="United States"]), 1)), size=6, angle = 0, color = "black") +
  xlab('Log(10)(# Lobbyists)') +
  ylab("Density\n") +
  ggtitle("All Lobbyists") +
  theme(legend.title = element_blank(), 
        legend.text = element_text(color = "black", size = 16),
        axis.text = element_text(color = "black", size = 16),
        axis.title = element_text(color = "black", face = "bold", size = 19),
        plot.title = element_text(color = "black", face = "bold", size = 21, hjust = 0.5),
        legend.position = "top",
        panel.border = element_blank(),
        axis.ticks.y=element_blank()) 


# In House Lobbyistsi
dens_lobs_ih <- ggplot(clients, aes(x=n_ihlob, fill=country)) +
  geom_density(alpha = 0.4, adjust = 0.7, size = 0, color = NA) +
  theme_bw() +
  scale_x_log10(breaks=c(0, 1, 10,100, 1000), limits = c(0.5,300)) +
  scale_y_continuous(breaks=c(seq(0,1.25, 0.25)), limits = c(0,1.25)) +
  scale_fill_manual("Country", 
                    values = c(gray.colors(3, 0.2, 0.6)[3],gray.colors(3, 0.2, 0.6)[1]),
                    labels = c("Canada", "United States")) +
  geom_vline(xintercept = mean(clients$n_ihlob[clients$country=="Canada"]), linetype = "dashed", color = gray.colors(3, 0.2, 0.6)[3], size = 1) +
  geom_vline(xintercept = mean(clients$n_ihlob[clients$country=="United States"]), linetype = "dashed", color = gray.colors(3, 0.2, 0.6)[1], size = 1) +
  annotate("text", x=mean(clients$n_ihlob[clients$country=="Canada"], na.rm = TRUE)-1.6, y=1, label=paste0(round(mean(clients$n_ihlob[clients$country=="Canada"], na.rm = TRUE), 1)), size=6, angle = 0, color = gray.colors(3, 0.2, 0.6)[2]) +
  annotate("text", x=mean(clients$n_ihlob[clients$country=="United States"],na.rm = TRUE)+2.1, y=1, label=paste0(round(mean(clients$n_ihlob[clients$country=="United States"], na.rm = TRUE), 1)), size=6, angle = 0, color = "black") +
  xlab('Log(10)(# Lobbyists)') +
  ylab("Density\n") +
  ggtitle("In-House Lobbyists") +
  theme(legend.title = element_blank(), 
        legend.text = element_text(color = "black", size = 16),
        axis.text = element_text(color = "black", size = 16),
        axis.title = element_text(color = "black", face = "bold", size = 19),
        axis.title.y = element_blank(),
        plot.title = element_text(color = "black", face = "bold", size = 21, hjust = 0.5),
        legend.position = "top",
        panel.border = element_blank(),
        axis.ticks.y=element_blank()) 


dens_lobs_xt <- ggplot(clients, aes(x=n_xtlob, fill=country)) +
  geom_density(alpha = 0.4, adjust = 0.7, size = 0, color = NA) +
  theme_bw() +
  scale_x_log10(breaks=c(0, 1, 10,100, 1000), limits = c(0.5,300)) +
  scale_y_continuous(breaks=c(seq(0,3, 0.5)), limits = c(0,3)) +
  scale_fill_manual("Country", 
                    values = c(gray.colors(3, 0.2, 0.6)[3],gray.colors(3, 0.2, 0.6)[1]),
                    labels = c("Canada", "United States")) +
  geom_vline(xintercept = mean(clients$n_xtlob[clients$country=="Canada"]), linetype = "dashed", color = gray.colors(3, 0.2, 0.6)[3], size = 1) +
  geom_vline(xintercept = mean(clients$n_xtlob[clients$country=="United States"]), linetype = "dashed", color = gray.colors(3, 0.2, 0.6)[1], size = 1) +
  annotate("text", x=mean(clients$n_xtlob[clients$country=="Canada"], na.rm = TRUE)-0.5, y=2.25, label=paste0(round(mean(clients$n_xtlob[clients$country=="Canada"], na.rm = TRUE), 1)), size=6, angle = 0, color = gray.colors(3, 0.2, 0.6)[2]) +
  annotate("text", x=mean(clients$n_xtlob[clients$country=="United States"], na.rm = TRUE)+1.3, y=2.25, label=paste0(round(mean(clients$n_xtlob[clients$country=="United States"], na.rm = TRUE), 1)), size=6, angle = 0, color = "black") +
  xlab('Log(10)(# Lobbyists)') +
  ylab("Density\n") +
  ggtitle("External Lobbyists") +
  theme(legend.title = element_blank(), 
        legend.text = element_text(color = "black", size = 16),
        axis.text = element_text(color = "black", size = 16),
        axis.title = element_text(color = "black", face = "bold", size = 19),
        axis.title.y = element_blank(),
        plot.title = element_text(color = "black", face = "bold", size = 21, hjust = 0.5),
        legend.position = "top",
        panel.border = element_blank(),
        axis.ticks.y=element_blank()) 

# Save plots
ggsave(dens_lobs_all, device = "pdf", dpi = 2400, filename = "../output/fig_b3a.pdf", width = 6.5, height = 5)
ggsave(dens_lobs_ih, device = "pdf", dpi = 2400, filename = "../output/fig_b3b.pdf", width = 6.5, height = 5)
ggsave(dens_lobs_xt, device = "pdf", dpi = 2400, filename = "../output/fig_b3c.pdf", width = 6.5, height = 5)




#### Table B2 ####

# read data, TPP lobbying, 2012-2020
Mobil <- readr::read_csv("../data/mobilization_ca_us_20211022_tpp_replic.csv")

### USA: Lobby with Association
### USA: Assoc Lobby
lm10 <- felm(I(us_tpp_assoc == 1) ~ factor(diff_osg_imp) + log(ind_sale) | 0 | 0 | 0, data = Mobil) # Lobby with Assoc
lm11 <- felm(I(us_tpp_assoc == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) | 0 | 0 | 0, data = Mobil) # Lobby with Assoc
lm12 <- felm(I(us_tpp_assoc == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) | naics2d | 0 | 0, data = Mobil) # Lobby Alone
### USA: Firm Lobby
lm14 <- felm(I(us_tpp_corp == 1) ~ factor(diff_osg_imp) + log(ind_sale) | 0 | 0 | 0, data = Mobil) # Lobby with Assoc
lm15 <- felm(I(us_tpp_corp == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) | 0 | 0 | 0, data = Mobil) # Lobby with Assoc
lm16 <- felm(I(us_tpp_corp == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) | naics2d | 0 | 0, data = Mobil) # Lobby Alone

cov2    <- vcovHC(lm10, type = "HC1") 
robust_se2   <- sqrt(diag(cov2))
cov3    <- vcovHC(lm11, type = "HC1") 
robust_se3   <- sqrt(diag(cov3))
cov4    <- vcovHC(lm12, type = "HC1") 
robust_se4   <- sqrt(diag(cov4))
cov6    <- vcovHC(lm14, type = "HC1") 
robust_se6   <- sqrt(diag(cov6))
cov7    <- vcovHC(lm15, type = "HC1") 
robust_se7   <- sqrt(diag(cov7))
cov8    <- vcovHC(lm16, type = "HC1") 
robust_se8   <- sqrt(diag(cov8))


stargazer::stargazer(lm10, lm11, lm12, lm14, lm15, lm16, type = "latex", title = "Mobilization during TPP in the United States", label = "tab_b2",
                     out = "../output/table_b2.tex",
                     covariate.labels = c("4-Firm Concentr. Ratio", "Mod.Differentiated","Differentiated", "Log(Sales)"), 
                     dep.var.labels = c("Association Mobilization (1/0)", "Firm Mobilization (1/0)"),
                     se = list(robust_se2, robust_se3, robust_se4,
                               robust_se6, robust_se7, robust_se8),
                     add.lines = list(c("2-digit NAICS FEs",  "", "", "$\\checkmark$", "", "", "$\\checkmark$")),
                     omit.stat = c("rsq", "ser"),
                     #omit = c("same_state", "cba", "majority", "subcom_chair", "com_chair", "power_com", "majority_leader",  "minority_leader"),
                     column.sep.width = "3pt", notes = "All estimates are OLS. Robust standard errors in parentheses.", notes.align = "l")


#### Table B3 ####

### CAN: Assoc Lobby
lm10 <- felm(I(ca_tpp_assoc == 1) ~ factor(diff_osg_imp) + log(ind_sale) | 0 | 0 | 0, data = Mobil) # Lobby with Assoc
lm11 <- felm(I(ca_tpp_assoc == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) | 0 | 0 | 0, data = Mobil) # Lobby with Assoc
lm12 <- felm(I(ca_tpp_assoc == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) | naics2d | 0 | 0, data = Mobil) # Lobby Alone
### CAN: Firm Lobby
lm14 <- felm(I(ca_tpp_corp == 1) ~ factor(diff_osg_imp) + log(ind_sale) | 0 | 0 | 0, data = Mobil) # Lobby with Assoc
lm15 <- felm(I(ca_tpp_corp == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) | 0 | 0 | 0, data = Mobil) # Lobby with Assoc
lm16 <- felm(I(ca_tpp_corp == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) | naics2d | 0 | 0, data = Mobil) # Lobby Alone

cov2    <- vcovHC(lm10, type = "HC1") 
robust_se2   <- sqrt(diag(cov2))
cov3    <- vcovHC(lm11, type = "HC1") 
robust_se3   <- sqrt(diag(cov3))
cov4    <- vcovHC(lm12, type = "HC1") 
robust_se4   <- sqrt(diag(cov4))
cov6    <- vcovHC(lm14, type = "HC1") 
robust_se6   <- sqrt(diag(cov6))
cov7    <- vcovHC(lm15, type = "HC1") 
robust_se7   <- sqrt(diag(cov7))
cov8    <- vcovHC(lm16, type = "HC1") 
robust_se8   <- sqrt(diag(cov8))


stargazer::stargazer(lm10, lm11, lm12, lm14, lm15, lm16, type = "latex", title = "Mobilization during TPP in Canada", label = "tab_b3",
                     out = "../output/table_b3.tex",
                     covariate.labels = c("4-Firm Concentr. Ratio", "Mod.Differentiated","Differentiated", "Log(Sales)"), 
                     dep.var.labels = c("Association Mobilization (1/0)", "Firm Mobilization (1/0)"),
                     se = list(robust_se2, robust_se3, robust_se4,
                               robust_se6, robust_se7, robust_se8),
                     add.lines = list(c("2-digit NAICS FEs",  "", "", "$\\checkmark$", "", "", "$\\checkmark$")),
                     omit.stat = c("rsq", "ser"),
                     #omit = c("same_state", "cba", "majority", "subcom_chair", "com_chair", "power_com", "majority_leader",  "minority_leader"),
                     column.sep.width = "3pt", notes = "All estimates are OLS. Robust standard errors in parentheses.", notes.align = "l")


rm(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8,
   lm9, lm10, lm11, lm12, lm13, lm14, lm15, lm16,
   cov1, cov2, cov3, cov4, cov5, cov6, cov7, cov8,
   robust_se1, robust_se2, robust_se3, robust_se4,
   robust_se5, robust_se6, robust_se7, robust_se8)



#### Table B4 ####
ca_lobs <- readr::read_csv("../data/ca_lobbyists_tpp.csv")
us_lobs <- readr::read_csv("../data/us_lobbyists_tpp.csv")


# Common Table w. USA/CAN for comparison. Leave out Peak Associations
us_lobs$us_xtlob_sh <- us_lobs$n_xtlob/us_lobs$n_lob
lm1 <- felm(us_xtlob_sh ~ log(restrictions)   | report_year | 0 | 0, data = us_lobs[us_lobs$org_type != "Peak Association",]) # 
lm2 <- felm(us_xtlob_sh ~ log(restrictions) + log(n_issues)  | report_year| 0 | 0, data = us_lobs[us_lobs$org_type != "Peak Association",]) # 
lm3 <- felm(us_xtlob_sh ~ log(restrictions) + log(n_issues) + log(ind_sale)   | report_year  | 0 | 0, data = us_lobs[us_lobs$org_type != "Peak Association",]) # 
lm4 <- felm(us_xtlob_sh ~ log(restrictions) + log(n_issues) + log(ind_sale)  | report_year + naics2 | 0 | 0, data = us_lobs[us_lobs$org_type != "Peak Association",]) # 

ca_lobs$ca_xtlob_sh <- ca_lobs$n_xtlob/ca_lobs$n_lob
lm5 <- felm(ca_xtlob_sh ~ log(restrictions)   | year | 0 | 0, data = ca_lobs[ca_lobs$org_type != "Peak Association",]) # 
lm6 <- felm(ca_xtlob_sh ~ log(restrictions) + log(n_issues)  | year| 0 | 0, data = ca_lobs[ca_lobs$org_type != "Peak Association",]) # 
lm7 <- felm(ca_xtlob_sh ~ log(restrictions) + log(n_issues) + log(ind_sale)   | year  | 0 | 0, data = ca_lobs[ca_lobs$org_type != "Peak Association",]) # 
lm8 <- felm(ca_xtlob_sh ~ log(restrictions) + log(n_issues) + log(ind_sale)  | year + naics2 | 0 | 0, data = ca_lobs[ca_lobs$org_type != "Peak Association",]) # 

cov1    <- vcovHC(lm1, type = "HC1") 
robust_se1   <- sqrt(diag(cov1))
cov2    <- vcovHC(lm2, type = "HC1") 
robust_se2   <- sqrt(diag(cov2))
cov3    <- vcovHC(lm3, type = "HC1") 
robust_se3   <- sqrt(diag(cov3))
cov4    <- vcovHC(lm4, type = "HC1") 
robust_se4   <- sqrt(diag(cov4))
cov5    <- vcovHC(lm5, type = "HC1") 
robust_se5   <- sqrt(diag(cov5))
cov6    <- vcovHC(lm6, type = "HC1") 
robust_se6   <- sqrt(diag(cov6))
cov7    <- vcovHC(lm7, type = "HC1") 
robust_se7   <- sqrt(diag(cov7))
cov8    <- vcovHC(lm8, type = "HC1") 
robust_se8   <- sqrt(diag(cov8))

stargazer::stargazer(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8, type = "latex", title = "External Lobbyist Usage during TPP Lobbying in the United States and Canada", label = "tab_b4",
                     out = "../output/table_b4.tex",
                     covariate.labels = c("Log(restrictions)", "Log(Issues Lobbied)", "Log(Sales)"), 
                     dep.var.labels = c("United States, Ext. Lobbyist Share", "Canada, Ext. Lobbyist Share"),
                     se = list(robust_se1, robust_se2, robust_se3, robust_se4,
                               robust_se5, robust_se6, robust_se7, robust_se8),
                     add.lines = list(c("Year FEs",  rep("$\\checkmark$", 8)),
                                      c("2-digit NAICS FEs",  "", "","", "$\\checkmark$", "", "","", "$\\checkmark$")),
                     omit.stat = c("rsq", "ser"),
                     #omit = c("same_state", "cba", "majority", "subcom_chair", "com_chair", "power_com", "majority_leader",  "minority_leader"),
                     column.sep.width = "3pt", notes = "Robust Standard Errors. All Estimates are OLS.", notes.align = "l")

rm(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8,
   cov1, cov2, cov3, cov4, cov5, cov6, cov7, cov8,
   robust_se1, robust_se2, robust_se3, robust_se4,
   robust_se5, robust_se6, robust_se7, robust_se8)



#### Figure C1 #####
gov_entities_n_all_selectfirms <- readr::read_csv("../data/instlob_us_usmca_tpp_clients.csv")


# reorder according to most lobbied instiutions
gov_entities_n_all_selectfirms$gov_entity <- factor(gov_entities_n_all_selectfirms$gov_entity, levels=gov_entities_n_all_selectfirms$gov_entity[order(gov_entities_n_all_selectfirms$n, decreasing=TRUE)])

fig_c1 <- ggplot(gov_entities_n_all_selectfirms[1:20,], aes(x = gov_entity, y = n/1000000)) +
  geom_col(position = position_dodge(0.8), width = 0.8) +
  scale_fill_grey() +
  theme_minimal() +
  xlab("\nInstitution") +
  ylab("Number of Times Reported to\nLobby Government Entity (in millions)\n") +
  theme(axis.text.x = element_text(face = "plain", colour = "black", size = 11, angle = 45, hjust = 1)) +
  theme(axis.text.y = element_text(face = "plain", colour = "black", size = 12)) +
  theme(axis.title.y = element_text(face = "bold", colour = "black", size = 14)) +
  theme(axis.title.x = element_blank()) +
  theme(legend.position="top") +
  theme(legend.title = element_blank()) +
  theme(legend.text =  element_text(face = "plain", colour = "black", size = 17))  

ggsave(fig_c1, filename = "../output/fig_c1.pdf", width = 9.25, height = 6.5)
rm(fig_c1)


#### Figure C2 ####
gov_entities_n_all <- readr::read_csv( "../data/instlob_us_all_clients.csv")

# reorder according to most lobbied instiutions
gov_entities_n_all$gov_entity <- factor(gov_entities_n_all$gov_entity, levels=gov_entities_n_all$gov_entity[order(gov_entities_n_all_selectfirms$n, decreasing=TRUE)])


library(ggplot2)  # GET TOP 20 INSTITUTIONS LOBBIED, ANALOG TO HUNEEUS/KIM
fig_c2 <- ggplot(gov_entities_n_all[1:20,], aes(x = gov_entity, y = n/1000000)) +
  geom_col(position = position_dodge(0.8), width = 0.8) +
  scale_fill_grey() +
  theme_minimal() +
  xlab("\nInstitution") +
  ylab("Number of Times Reported to\nLobby Government Entity (in millions)\n") +
  theme(axis.text.x = element_text(face = "plain", colour = "black", size = 11, angle = 45, hjust = 1)) +
  theme(axis.text.y = element_text(face = "plain", colour = "black", size = 12)) +
  theme(axis.title.y = element_text(face = "bold", colour = "black", size = 14)) +
  theme(axis.title.x = element_blank()) +
  theme(legend.position="top") +
  theme(legend.title = element_blank()) +
  theme(legend.text =  element_text(face = "plain", colour = "black", size = 17)) 

ggsave(fig_c2, filename = "../output/fig_c2.pdf", width = 9.25, height = 6.5)
rm(fig_c2)


#### Figure D1 ####
gov_entities_n_all_selectfirms <- readr::read_csv("../data/instlob_ca_usmca_tpp_clients.csv")

# reorder according to most lobbied instiutions
gov_entities_n_all_selectfirms$gov_entity <- factor(gov_entities_n_all_selectfirms$gov_entity, levels=gov_entities_n_all_selectfirms$gov_entity[order(gov_entities_n_all_selectfirms$n, decreasing=TRUE)])

fig_d1 <- ggplot(gov_entities_n_all_selectfirms[1:20,], aes(x = gov_entity, y = n/1000)) +
  geom_col(position = position_dodge(0.8), width = 0.8) +
  scale_fill_grey() +
  theme_minimal() +
  xlab("\nInstitution") +
  ylab("Number of Times Reported to\nLobby Government Entity (in thousands)\n") +
  theme(axis.text.x = element_text(face = "plain", colour = "black", size = 11, angle = 45, hjust = 1)) +
  theme(axis.text.y = element_text(face = "plain", colour = "black", size = 12)) +
  theme(axis.title.y = element_text(face = "bold", colour = "black", size = 14)) +
  theme(axis.title.x = element_blank()) +
  theme(legend.position="top") +
  theme(legend.title = element_blank()) +
  theme(legend.text =  element_text(face = "plain", colour = "black", size = 17))  

ggsave(fig_d1, filename = "../output/fig_d1.pdf", width = 9.25, height = 6.5)
rm(fig_d1)
# clean up



#### Figure D2 ####
gov_entities_n_all <- readr::read_csv( "../data/instlob_ca_all_clients.csv")

# reorder according to most lobbied instiutions
gov_entities_n_all$gov_entity <- factor(gov_entities_n_all$gov_entity, levels=gov_entities_n_all$gov_entity[order(gov_entities_n_all_selectfirms$n, decreasing=TRUE)])

library(ggplot2)  # GET TOP 20 INSTITUTIONS LOBBIED, ANALOG TO HUNEEUS/KIM
fig_d2 <- ggplot(gov_entities_n_all[1:20,], aes(x = gov_entity, y = n/1000)) +
  geom_col(position = position_dodge(0.8), width = 0.8) +
  scale_fill_grey() +
  theme_minimal() +
  xlab("\nInstitution") +
  ylab("Number of Times Reported to\nLobby Government Entity (in thousands)\n") +
  theme(axis.text.x = element_text(face = "plain", colour = "black", size = 11, angle = 45, hjust = 1)) +
  theme(axis.text.y = element_text(face = "plain", colour = "black", size = 12)) +
  theme(axis.title.y = element_text(face = "bold", colour = "black", size = 14)) +
  theme(axis.title.x = element_blank()) +
  theme(legend.position="top") +
  theme(legend.title = element_blank()) +
  theme(legend.text =  element_text(face = "plain", colour = "black", size = 17)) 

ggsave(fig_d2, filename = "../output/fig_d2.pdf", width = 9.25, height = 6.5)
rm(fig_d2)


#### Table E1 ####
# read data
Mobil <- readr::read_csv("../data/mobilization_ca_us_20211022.csv")

### USA: Assoc Lobby
glm2 <- glm(I(us_usmca_assoc == 1) ~ factor(diff_osg_imp) + log(ind_sale), data = Mobil) # Lobby with Assoc
glm3 <- glm(I(us_usmca_assoc == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp), data = Mobil) # Lobby with Assoc
glm4 <- glm(I(us_usmca_assoc == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) + as.factor(naics2d), data = Mobil) # Lobby Alone
### USA: Firm Lobby
glm6 <- glm(I(us_usmca_corp == 1) ~ factor(diff_osg_imp) + log(ind_sale), data = Mobil) # Lobby with Assoc
glm7 <- glm(I(us_usmca_corp == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp), data = Mobil) # Lobby with Assoc
glm8 <- glm(I(us_usmca_corp == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) + as.factor(naics2d), data = Mobil) # Lobby Alone



cov2    <- vcovHC(glm2, type = "HC1") 
robust_se2   <- sqrt(diag(cov2))
cov3    <- vcovHC(glm3, type = "HC1") 
robust_se3   <- sqrt(diag(cov3))
cov4    <- vcovHC(glm4, type = "HC1") 
robust_se4   <- sqrt(diag(cov4))
cov6    <- vcovHC(glm6, type = "HC1") 
robust_se6   <- sqrt(diag(cov6))
cov7    <- vcovHC(glm7, type = "HC1") 
robust_se7   <- sqrt(diag(cov7))
cov8    <- vcovHC(glm8, type = "HC1") 
robust_se8   <- sqrt(diag(cov8))

stargazer::stargazer(glm2, glm3, glm4, glm6, glm7, glm8, type = "latex", title = "Mobilization during USMCA, United States, Logistic Regressions", label = "tab_e1",
                     out = "../output/table_e1.tex",
                     covariate.labels = c("4-Firm Concentr. Ratio", "Mod.Differentiated","Differentiated", "Log(Sales)"), 
                     dep.var.labels = c("Association Mobilization (1/0)", "Firm Mobilization (1/0)"),
                     omit = c("naics2"),
                     se = list(robust_se2, robust_se3, robust_se4,
                               robust_se6, robust_se7, robust_se8),
                     add.lines = list(c("2-digit NAICS FEs",  "", "","", "$\\checkmark$", "", "","", "$\\checkmark$")),
                     omit.stat = c("rsq", "ser"),
                     #omit = c("same_state", "cba", "majority", "subcom_chair", "com_chair", "power_com", "majority_leader",  "minority_leader"),
                     column.sep.width = "3pt", notes = "Robust Standard Errors. All Estimates are Logistic Regressions", notes.align = "l")



#### Table E2 ####

### CAN: Assoc Lobby
glm2 <- glm(I(ca_usmca_assoc == 1) ~ factor(diff_osg_imp) + log(ind_sale), data = Mobil) # Lobby with Assoc
glm3 <- glm(I(ca_usmca_assoc == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp), data = Mobil) # Lobby with Assoc
glm4 <- glm(I(ca_usmca_assoc == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) + as.factor(naics2d), data = Mobil) # Lobby Alone
### CAN: Firm Lobby
glm6 <- glm(I(ca_usmca_corp == 1) ~ factor(diff_osg_imp) + log(ind_sale), data = Mobil) # Lobby with Assoc
glm7 <- glm(I(ca_usmca_corp == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp), data = Mobil) # Lobby with Assoc
glm8 <- glm(I(ca_usmca_corp == 1) ~ CR4 + log(ind_sale) + factor(diff_osg_imp) + as.factor(naics2d), data = Mobil) # Lobby Alone

cov2    <- vcovHC(glm2, type = "HC1") 
robust_se2   <- sqrt(diag(cov2))
cov3    <- vcovHC(glm3, type = "HC1") 
robust_se3   <- sqrt(diag(cov3))
cov4    <- vcovHC(glm4, type = "HC1") 
robust_se4   <- sqrt(diag(cov4))
cov6    <- vcovHC(glm6, type = "HC1") 
robust_se6   <- sqrt(diag(cov6))
cov7    <- vcovHC(glm7, type = "HC1") 
robust_se7   <- sqrt(diag(cov7))
cov8    <- vcovHC(glm8, type = "HC1") 
robust_se8   <- sqrt(diag(cov8))

stargazer::stargazer(glm2, glm3, glm4,glm6, glm7, glm8, type = "latex", title = "Mobilization during USMCA, Canada, Logistic Regressions", label = "tab_e2",
                     out = "../output/table_e2.tex",
                     covariate.labels = c("4-Firm Concentr. Ratio", "Mod.Differentiated","Differentiated", "Log(Sales)"), 
                     dep.var.labels = c("Association Mobilization (1/0)", "Firm Mobilization (1/0)"),
                     omit = c("naics2"),
                     se = list(robust_se2, robust_se3, robust_se4,
                               robust_se6, robust_se7, robust_se8),
                     add.lines = list(c("2-digit NAICS FEs",  "", "","", "$\\checkmark$", "", "","", "$\\checkmark$")),
                     omit.stat = c("rsq", "ser"),
                     #omit = c("same_state", "cba", "majority", "subcom_chair", "com_chair", "power_com", "majority_leader",  "minority_leader"),
                     column.sep.width = "3pt", notes = "Robust Standard Errors. All Estimates are Logistic Regressions", notes.align = "l")


#### Table E3 ####
library(sandwich)
library(lmtest)
ca_lobs <- readr::read_csv("../data/ca_lobbyists.csv")
us_lobs <- readr::read_csv("../data/us_lobbyists.csv")

# Logistic Regresssions
glm1 <- glm(xtlob ~ log(restrictions) + as.factor(report_year), data = us_lobs[us_lobs$org_type != "Peak Association",]) # 
glm2 <- glm(xtlob ~ log(restrictions) + log(n_issues) + as.factor(report_year), data = us_lobs[us_lobs$org_type != "Peak Association",]) # 
glm3 <- glm(xtlob ~ log(restrictions) + log(n_issues)  + log(ind_sale) + as.factor(report_year), data = us_lobs[us_lobs$org_type != "Peak Association",]) # 
glm4 <- glm(xtlob ~ log(restrictions) + log(n_issues)  + log(ind_sale) + as.factor(report_year) + as.factor(naics2), data = us_lobs[us_lobs$org_type != "Peak Association",]) # 

glm5 <- glm(xtlob ~ log(restrictions) + as.factor(year), data = ca_lobs[ca_lobs$org_type != "Peak Association",]) # 
glm6 <- glm(xtlob ~ log(restrictions) + log(n_issues) + as.factor(year), data = ca_lobs[ca_lobs$org_type != "Peak Association",]) # 
glm7 <- glm(xtlob ~ log(restrictions) + log(n_issues)  + log(ind_sale) + as.factor(year), data = ca_lobs[ca_lobs$org_type != "Peak Association",]) # 
glm8 <- glm(xtlob ~ log(restrictions) + log(n_issues)  + log(ind_sale) + as.factor(year) + as.factor(naics2), data = ca_lobs[ca_lobs$org_type != "Peak Association",]) # 


cov1    <- vcovHC(glm1, type = "HC1") 
robust_se1   <- sqrt(diag(cov1))
cov2    <- vcovHC(glm2, type = "HC1") 
robust_se2   <- sqrt(diag(cov2))
cov3    <- vcovHC(glm3, type = "HC1") 
robust_se3   <- sqrt(diag(cov3))
cov4    <- vcovHC(glm4, type = "HC1") 
robust_se4   <- sqrt(diag(cov4))
cov5    <- vcovHC(glm5, type = "HC1") 
robust_se5   <- sqrt(diag(cov5))
cov6    <- vcovHC(glm6, type = "HC1") 
robust_se6   <- sqrt(diag(cov6))
cov7    <- vcovHC(glm7, type = "HC1") 
robust_se7   <- sqrt(diag(cov7))
cov8    <- vcovHC(glm8, type = "HC1") 
robust_se8   <- sqrt(diag(cov8))

stargazer::stargazer(glm1, glm2, glm3, glm4, glm5, glm6, glm7, glm8, type = "latex", title = "External Lobbyist Usage during USMCA Lobbying in the United States and Canada", label = "tab_e3",
                     out = "../output/table_e3.tex",
                     covariate.labels = c("Log(restrictions)", "Log(Issues Lobbied)", "Log(Sales)"), 
                     omit = c("naics2", "year", "report_year"),
                     se = list(robust_se1, robust_se2, robust_se3, robust_se4,
                               robust_se5, robust_se6, robust_se7, robust_se8),
                     dep.var.labels = c("United States, Ext. Lobbyist (1/0)", "Canada, Ext. Lobbyist (1/0)"),
                     add.lines = list(c("Year FEs",  rep("$\\checkmark$", 8)),
                                      c("2-digit NAICS FEs",  "", "","", "$\\checkmark$", "", "","", "$\\checkmark$")),
                     omit.stat = c("rsq", "ser"),
                     #omit = c("same_state", "cba", "majority", "subcom_chair", "com_chair", "power_com", "majority_leader",  "minority_leader"),
                     column.sep.width = "3pt", notes = "Robust Standard Errors. All Estimates are Logistic Regressions", notes.align = "l")


#### clean up ####
rm(list = ls())

